Completed
Pull Request — master (#266)
by Sergey
02:26
created

ArrayHelper::getValueByKey()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
nc 4
nop 3
dl 0
loc 16
rs 9.2
c 1
b 0
f 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers;
4
5
class ArrayHelper
6
{
7
    /**
8
     * @param string $key
9
     * @param array $data
10
     * @param mixed $default
11
     * @return array|bool|mixed
12
     */
13
    public static function getValueByKey($key = '', $data, $default = null)
14
    {
15
        if(empty($key)) return $data;
16
17
        $indexes = explode('.', $key);
18
19
        $value = $data;
20
21
        foreach ($indexes as $index) {
22
            if(!isset($value[$index])) return $default;
23
24
            $value = $value[$index];
25
        }
26
27
        return $value;
28
    }
29
}