我有以下数组:
Array
(
[0] => Array
(
[type] => foo
)
[1] => Array
(
[type] => bar
)
[2] => Array
(
[type] => bar
)
)
并且需要知道是否存在一个或多个type
,其值为bar
,不这样做:
foreach ($arrayOfTypes as $type) {
if ($type['type'] == 'bar')
{
// Stuff here
}
}
(仅供学习)
最佳答案
我会选择 array_filter()
;
$filteredArray = array_filter($stuff, function($item){
return $item['type'] == 'bar';
});
if( count($filteredArray) > 0 ){
echo 'There was a bar item in array.';
}else{
echo 'No luck sorry!';
}
关于php - 检查多维数组中的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12443317/