I am trying to count how many times Apple appears in this array
$fruits = array (
"Apple",
array("Apple", "Kiwi", "Tomato")
);
Is this a multidimension array? How would I be able to check the total count apple appears?
I am trying to count how many times Apple appears in this array
$fruits = array (
"Apple",
array("Apple", "Kiwi", "Tomato")
);
Is this a multidimension array? How would I be able to check the total count apple appears?
First you need to flat the array.
$fruits = array (
"Apple",
array("Apple", "Kiwi", "Tomato")
);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($fruits));
foreach($it as $v) {
$flat_arr[] = $v;
}
Then filter items that only have 'Apple':
$result_count = count(array_filter($flat_arr, function($item) {
return substr('Apple', $item) !== false;
}));