计算PHP中具有特定值的子数组总数
我想计算$example的子数组中有多少包含值为其他的元素 最简单的方法是什么?是您需要的:计算PHP中具有特定值的子数组总数,php,arrays,Php,Arrays,我想计算$example的子数组中有多少包含值为其他的元素 最简单的方法是什么?是您需要的: $example = array 'test' => array( 'something' => 'value' ), 'whatever' => array( 'something' => 'other' ), 'blah' => array(
$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函数不知道您的变量-如果您想使用变量,请参阅我的编辑。