Created
February 23, 2016 17:44
-
-
Save zephster/eeda67968dda6561d3b2 to your computer and use it in GitHub Desktop.
_recursive_array_search php
This file contains hidden or 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
// returns null if $needle is not found anywhere in $haystack, | |
// or an array of $haystack keys to reference $needle where it is located within $haystack | |
// eg. [0]departure [1]connecting [2]0 => $haystack[departure][connecting][0] == $needle | |
private function _recursive_array_search($needle, $haystack) | |
{ | |
foreach ($haystack as $key => $value) | |
{ | |
if ($needle === $value) | |
{ | |
return array($key); | |
} | |
elseif (is_array($value) && $subkey = $this->_recursive_array_search($needle, $value)) | |
{ | |
array_unshift($subkey, $key); | |
return $subkey; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment