$example =
array
'test' =>
array(
'something' => 'value'
),
'whatever' =>
array(
'something' => 'other'
),
'blah' =>
array(
'something' => 'other'
)
);
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()
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;
}));
You can try the following:
$count = 0;
foreach( $example as $value ) {
if( in_array("other", $value ) )
$count++;
}