Array get path

With this function you can find values in an array by <br/> an path like "abc/def/ghi". <br/> This function was an idea, I had a few months ago...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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)
// test data:
 
$tree_data = array(
    'red' => array(
        'max' => 100
        'min' => 0
    ),
    'blue' => 'water',
    'green' => 'flowers',
    'grey' => array(
        'light' => array('eeeeee''e7e7e7'),
        'dark' => array('cccccc')
    ),
    7 => 'foobar'
);
 
 
// path tests:
 
$path_tests = array(
    'red/max'
    'foo/bar',
    'grey/light/1',
    'grey/dark',
    'grey/dark/1',
    'red/min',
X

Url: http://www.jonasjohn.de/snippets/php/array-get-path.htm

Language: PHP | User: ShareMySnippets | Created: Oct 16, 2013