Skip to content

Instantly share code, notes, and snippets.

@mattboon
Created November 9, 2012 09:22
Show Gist options
  • Save mattboon/4044735 to your computer and use it in GitHub Desktop.
Save mattboon/4044735 to your computer and use it in GitHub Desktop.
PHP - in_array equivalent for recursive arrays (containing stdClass objects)
<?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;
}
?>
@xeoncross
Copy link

Seems like you should also be able to do this with array_map() or one of the other functions called recursively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment