Skip to content

Instantly share code, notes, and snippets.

@zephster
Created February 23, 2016 17:44
Show Gist options
  • Save zephster/eeda67968dda6561d3b2 to your computer and use it in GitHub Desktop.
Save zephster/eeda67968dda6561d3b2 to your computer and use it in GitHub Desktop.
_recursive_array_search php
// 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