Array get path

With this function you can find values in an array by
an path like "abc/def/ghi".
This function was an idea, I had a few months ago...

Snippet information

Author:
Jonas John

License:
Public Domain

Language:
PHP

Created:
04/27/2006

Updated:
04/27/2006

Tags:


function ArrayGetPath($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;
}
 
// Please look at the examples (below)


Found a bug? Or do you have a better solution for this?
Feel free to leave a message:

Add a comment


Leave a comment

Peter B. February 15, 2008 at 12:38
Awesome! It's like XPath for arrays!

That's a pretty cool function there.