$example =
array
'test' =>
array(
'something' => 'value'
),
'whatever' =>
array(
'something' => 'other'
),
'blah' =>
array(
'something' => 'other'
)
);
Run Code Online (Sandbox Code Playgroud)
我想要计算有多少$example个子数组包含一个带有值的元素other.
这样做最简单的方法是什么?
array_filter() 是你需要的:
count(array_filter($example, function($element){
return $element['something'] == 'other';
}));
Run Code Online (Sandbox Code Playgroud)
如果您想要更灵活:
$key = 'something';
$value = 'other';
$c = count(array_filter($example, function($element) use($key, $value){
return $element[$key] == $value;
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2614 次 |
| 最近记录: |