function array_get_path($data, $path, &$result){
$found = true;
$path = explode("/", $path);
for ($x=0; ($x < count($path) and $found); $x++){
$key = $path[$x];
if (isset($data[$key])){
$data = $data[$key];
}
else { $found = false; }
}
$result = $data;
return $found;
}
// test array:
$tree = array(
'red' => array(),
'blue' => 'water',
'green' => 'flowers',
'grey' => array(
'light' => array('eeeeee', 'e7e7e7'),
'dark' => array('cccccc')
),
7 => 'foobar'
);
// path tests:
if (array_get_path($tree, 'grey/light/1', $result)){
print $result;
// should print --> e7e7e7
}
array_get_path
Leave a Reply
You must be logged in to post a comment.