ArrayUtility::flatten()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4555
c 0
b 0
f 0
cc 5
nc 7
nop 2
1
<?php
2
declare(strict_types = 1);
3
namespace T3G\Elasticorn\Utility;
4
5
6
/**
7
 * Class ArrayUtility
8
 *
9
 * @package T3G\Elasticorn\Utility
10
 */
11
class ArrayUtility
12
{
13
14
    /**
15
     * @param array $array
16
     * @param string $prefix
17
     * @return array
18
     */
19
    public static function flatten(array $array, string $prefix = '') : array
20
    {
21
        $result = [];
22
        foreach ($array as $key => $value) {
23
            if ($prefix) {
24
                $key = $prefix . '.' . $key;
25
            }
26
            if (is_array($value)) {
27
                $result = array_merge($result, self::flatten($value, (string)$key));
28
            } else {
29
                $result[$key] = is_scalar($value) ? $value : gettype($value);
30
            }
31
        }
32
        return $result;
33
    }
34
}