Skip to content

Instantly share code, notes, and snippets.

@avdg
Forked from anonymous/gist:858611
Created March 7, 2011 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avdg/858629 to your computer and use it in GitHub Desktop.
Save avdg/858629 to your computer and use it in GitHub Desktop.
#!/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