如果在php中包含特定项,如何从多维数组中删除数组 [英] how to remove an array from multi dimensional array if include specific item in php
本文介绍了如果在php中包含特定项,如何从多维数组中删除数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
之前:
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
我要删除包含" BMW "作为项的整个数组
i want remove whole array witch include "BMW" as an item
结果如下:
$cars = array
(
array("Volvo",22,18),
array("Saab",5,2),
array("Land Rover",17,15)
);
推荐答案
您可以使用 array_filter
即可根据自定义逻辑从数组中删除元素.如果应分别保留或删除元素,则回调函数应返回 true
或 false
.
$filter = 'BMW';
$cars = array_filter($cars, function($car) use ($filter) {
return $car[0] !== $filter;
});
在这里,我们正在过滤掉第一个元素等于 $ filter
变量的所有行.
Here, we're filtering out all rows where the first element is equal to the $filter
variable.
这篇关于如果在php中包含特定项,如何从多维数组中删除数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文