总共算子阵在PHP中的某些值 [英] Count total of subarrays with certain values in PHP
本文介绍了总共算子阵在PHP中的某些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$example =
array
'test' =>
array(
'something' => 'value'
),
'whatever' =>
array(
'something' => 'other'
),
'blah' =>
array(
'something' => 'other'
)
);
我要统计有多少 $例如'S
子阵列包含带有值的元素的等
。
I want to count how many of $example
's subarrays contain an element with the value other
.
什么是去这样做最简单的方法?
What's the easiest way to go about doing this?
推荐答案
array_filter()
是你所需要的:
array_filter()
is what you need:
count(array_filter($example, function($element){
return $element['something'] == 'other';
}));
如果你想成为更灵活的:
In case you want to be more flexible:
$key = 'something';
$value = 'other';
$c = count(array_filter($example, function($element) use($key, $value){
return $element[$key] == $value;
}));
这篇关于总共算子阵在PHP中的某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文