Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
93 - 1
<?php namespace Fuse\Helpers;
2
 
3
function deep_value($assoc, $path, &$list = [])
4
{
5
    if (!$path) {
6
        // If there's no path left, we've gotten to the object we care about.
7
        $list[] = $assoc;
8
    } else {
9
        $dotIndex = mb_strpos($path, '.');
10
        $firstSegment = $path;
11
        $remaining = null;
12
 
13
        if ($dotIndex !== false) {
14
            $firstSegment = mb_substr($path, 0, $dotIndex);
15
            $remaining = mb_substr($path, $dotIndex + 1);
16
        }
17
 
18
        $value = isset($assoc[$firstSegment])
19
            ? $assoc[$firstSegment]
20
            : null;
21
 
22
        if (!is_null($value)) {
23
            if (!$remaining && (is_string($value) || is_int($value) || is_float($value))) {
24
                $list[] = (string) $value;
25
            } elseif (is_list($value)) {
26
                // Search each item in the array.
27
                for ($i = 0, $len = sizeof($value); $i < $len; $i++) {
28
                    deep_value($value[$i], $remaining, $list);
29
                }
30
            } elseif ($remaining) {
31
                // An associative array. Recurse further.
32
                deep_value($value, $remaining, $list);
33
            }
34
        }
35
    }
36
 
37
    return $list;
38
}