gpt4 book ai didi

php - 检查多维数组中的键值对

转载 作者:行者123 更新时间:2023-12-02 01:15:10 24 4
gpt4 key购买 nike

我有以下数组:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com