Skip to content

Instantly share code, notes, and snippets.

@Cvetomird91
Last active May 16, 2017 11:40
Show Gist options
  • Save Cvetomird91/fe1c02ebafecfa504b46a8b94f22e382 to your computer and use it in GitHub Desktop.
Save Cvetomird91/fe1c02ebafecfa504b46a8b94f22e382 to your computer and use it in GitHub Desktop.
<?php
class ArraySearch
{
public static $occurences = 0;
public static function countItems($arr, $item)
{
foreach ($arr as $key => $value) {
if (is_array($value)) {
ArraySearch::countItems($value, $item);
} else {
if ($value === $item) {
ArraySearch::$occurences++;
continue;
}
}
}
return ArraySearch::$occurences;
}
}
$arr = [
"apple",
["banana", "strawberry", "apple"],
["burrberry", "grapes"],
["apple", "cherry", ["melon", "apple"]],
["apple", "cherry", ["melon", ["apple"]]],
"tomato"
];
echo ArraySearch::countItems($arr, "apple");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment