Created
March 28, 2011 15:35
-
-
Save EdwardIII/890668 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
function find_person($value, $search_term){ | |
foreach($value as $category){ | |
if (isset($category['children'])) { | |
return find_person($category['children'], $search_term); | |
} else { | |
if ($category['name'] == $search_term) return $category['person_id']; | |
} | |
} | |
} | |
$AoH = array( | |
'name' => 'beavis', | |
'person_id' => 'bvs', | |
'children' => array( | |
array ('name' => 'dave', 'person_id' => 'dve', | |
'children' => array( | |
array( | |
'name' => 'cornelius', | |
'person_id' => 'crn' | |
) | |
) | |
), | |
array ('name' => 'cornholio', 'person_id' => 'bth') | |
), | |
); | |
$the_person_id_i_want = find_person($AoH['children'], 'cornholio'); | |
var_dump($the_person_id_i_want); | |
?> |
Author
EdwardIII
commented
Mar 28, 2011
'beavis',
'person_id' => 'bvs',
'children' => array(
array ('name' => 'cornholio', 'person_id' => 'bth'),
array ('name' => 'dave', 'person_id' => 'dve')
)
);
$the_person_id_i_want = find_person($AoH, 'cornholio');
var_dump($the_person_id_i_want);
?>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment