Created
November 9, 2012 09:22
-
-
Save mattboon/4044735 to your computer and use it in GitHub Desktop.
PHP - in_array equivalent for recursive arrays (containing stdClass objects)
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 in_array_r($needle, $haystack) { | |
$found = false; | |
foreach ($haystack as $item) { | |
if ($item === $needle) { | |
$found = true; | |
break; | |
} elseif (is_array($item)) { | |
$found = in_array_r($needle, $item); | |
if($found) { | |
break; | |
} | |
} | |
} | |
return $found; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems like you should also be able to do this with
array_map()
or one of the other functions called recursively.