Skip to content

Instantly share code, notes, and snippets.

@Fustrate
Created August 17, 2011 03:34
Show Gist options
  • Save Fustrate/1150762 to your computer and use it in GitHub Desktop.
Save Fustrate/1150762 to your computer and use it in GitHub Desktop.
Find a deep array value given the path as an array. Please tell me this isn't the most efficient way...
<?php
function find_it($key)
{
$preserve_key = $key;
$txt['first']['second']['third']['end'] = 'yay!';
$search = $txt;
while (($part = array_shift($key)) && isset($search[$part]))
$search = $search[$part];
if (is_string($search))
echo 'Found: ' . $search . '<br />';
else
echo 'Did not find: ' . implode('+', $preserve_key) . '<br />';
}
find_it(array('first', 'second', 'third', 'end'));
find_it(array('first', 'second', 'third', 'end2'));
/*
Outputs:
Found: yay!
Did not find: first+second+third+end2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment