-
-
Save avdg/858629 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/php | |
<?php | |
function find_person($array, $key, $search_term) | |
{ | |
// We are given 1 element | |
if (isset($array['name']) { | |
return find_person_helper($array, $key, $search_term); | |
} | |
// We are given multiple elements | |
foreach ($array as $key2 => $value) { | |
// walk through elements | |
if (($result = find_person_helper($value, $key, $search) !== false) { | |
return $result; | |
} | |
} | |
// none found | |
return false; | |
} | |
// Stuff for later, ignore this for now | |
function find_person_helper($array, $key, $search_term) | |
{ | |
if ($value == $search_term) { | |
print "Found $search_term in $key\n"; | |
return $the_person_id_in_the_same_array_as_this_term; | |
} | |
return find_person($value['children'], $key, $search); | |
} | |
$AoH = array( | |
'name' => 'beavis', | |
'person_id' => 'bvs', | |
'children' => array( | |
array( | |
'name' => 'cornholio', | |
'person_id' => 'bth', | |
), | |
), | |
); | |
$the_person_id_i_want = array_walk_recursive($AoH, 'find_person', 'cornholio'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment