Skip to content

Instantly share code, notes, and snippets.

@EdwardIII
Created March 28, 2011 15:35
Show Gist options
  • Save EdwardIII/890668 to your computer and use it in GitHub Desktop.
Save EdwardIII/890668 to your computer and use it in GitHub Desktop.
<?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);
?>
@EdwardIII
Copy link
Author

'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