在PHP中计算具有特定值的子阵列的总数

Use*_*upt 1 php arrays

$example = 
  array
    'test' =>
      array(
        'something' => 'value'
      ),
    'whatever' =>
      array(
        'something' => 'other'
      ),
    'blah' =>
      array(
        'something' => 'other'
      )
  );
Run Code Online (Sandbox Code Playgroud)

我想要计算有多少$example个子数组包含一个带有值的元素other.

这样做最简单的方法是什么?

moo*_*e99 6

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)