I am trying to find out a way of counting number of occurrences in a multidimensional array. The array looks like this:
$arr = [
"apple",
["banana", "strawberry", "apple"],
["banana", "strawberry", "apple", ["banana", "strawberry", "apple"]]
];
The code that i have got is this:
$count = 0;
foreach ($arr as $arritem) {
if ($arritem === "apple") {
$count++;
}
}
echo $count;
I get the output of q when I search for "Apple". Not sure what am I doing wrong here. I need the output to be 4. Any help?
Thanks.
See Question&Answers more detail:os