Completed
Push — master ( 96ab65...e04c91 )
by Алексей
09:52
created

helpers.php ➔ data_get()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 41
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 9
nop 3
dl 0
loc 41
ccs 0
cts 0
cp 0
crap 90
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
if ( ! function_exists('array_get'))
4
{
5
    /**
6
     * Get an item from an array using "dot" notation.
7
     *
8
     * @param  array   $array
9
     * @param  string  $key
10
     * @param  mixed   $default
11
     * @return mixed
12
     */
13
    function array_get($array, $key, $default = null)
14
    {
15
        if (is_null($key)) return $array;
16
17
        if (isset($array[$key])) return $array[$key];
18
19
        foreach (explode('.', $key) as $segment)
20
        {
21
            if ( ! is_array($array) || ! array_key_exists($segment, $array))
22
            {
23
                return value($default);
24
            }
25
26
            $array = $array[$segment];
27
        }
28
29
        return $array;
30
    }
31
}
32