Last active
May 16, 2017 11:40
-
-
Save Cvetomird91/fe1c02ebafecfa504b46a8b94f22e382 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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