Created
September 3, 2019 23:39
-
-
Save dasider41/a86be56f01cfe0f4f5a73e0811f0fda3 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 | |
function arrayFlat(array $array): array | |
{ | |
return array_reduce($array, function ($acc, $item) { | |
return array_merge($acc, is_array($item) ? arrayFlat($item) : [$item]); | |
}, []); | |
} | |
function countItems(array $arr, string $item): int | |
{ | |
return array_count_values(arrayFlat($arr))[$item]; | |
} | |
$arr = [ | |
"apple", | |
[ | |
"banana", "strawberry", "apple", | |
[ | |
"banana", "strawberry", "apple", | |
[ | |
"banana", "strawberry", "apple" | |
] | |
] | |
] | |
]; | |
var_dump(countItems($arr, "apple") === 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment