Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算PHP中具有特定值的子数组总数_Php_Arrays - Fatal编程技术网

计算PHP中具有特定值的子数组总数

计算PHP中具有特定值的子数组总数,php,arrays,Php,Arrays,我想计算$example的子数组中有多少包含值为其他的元素 最简单的方法是什么?是您需要的: $example = array 'test' => array( 'something' => 'value' ), 'whatever' => array( 'something' => 'other' ), 'blah' => array(

我想计算$example的子数组中有多少包含值为其他的元素

最简单的方法是什么?

是您需要的:

$example = 
  array
    'test' =>
      array(
        'something' => 'value'
      ),
    'whatever' =>
      array(
        'something' => 'other'
      ),
    'blah' =>
      array(
        'something' => 'other'
      )
  );
如果您希望更加灵活:

count(array_filter($example, function($element){

    return $element['something'] == 'other';

}));
这是您需要的:

$example = 
  array
    'test' =>
      array(
        'something' => 'value'
      ),
    'whatever' =>
      array(
        'something' => 'other'
      ),
    'blah' =>
      array(
        'something' => 'other'
      )
  );
如果您希望更加灵活:

count(array_filter($example, function($element){

    return $element['something'] == 'other';

}));

您可以尝试以下操作:

$key = 'something';
$value = 'other';

$c = count(array_filter($example, function($element) use($key, $value){

    return $element[$key] == $value;

}));

您可以尝试以下操作:

$key = 'something';
$value = 'other';

$c = count(array_filter($example, function($element) use($key, $value){

    return $element[$key] == $value;

}));

这一个对我来说是有效的,但当我尝试用变量替换其他变量时,它只会给我一个结果0:/这是因为lambda函数不知道你的变量-如果你想使用变量,请参阅我的编辑。这一个对我来说是有效的,但是当我尝试用变量替换其他变量时,它只给了我一个结果0:/这是因为lambda函数不知道您的变量-如果您想使用变量,请参阅我的编辑。