Completed
Push — master ( dfd5cb...5cac00 )
by Tim
09:24
created

Classes/Utility/ArrayUtility.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Arrays utility.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Autoloader\Utility;
9
10
/**
11
 * Arrays utility.
12
 */
13
class ArrayUtility
14
{
15
    /**
16
     * Set a node in the array.
17
     *
18
     * @param array $data
19
     * @param array $array
20
     *
21
     * @see http://www.php.net/manual/de/function.array-walk-recursive.php#106340
22
     */
23
    public static function setNodes(array $data, array &$array)
24
    {
25
        $separator = '|';
26
        foreach ($data as $name => $value) {
27
            if (false === \mb_strpos($name, $separator)) {
28
                $array[$name] = $value;
29
            } else {
30
                $keys = \explode($separator, $name);
31
                $optTree = &$array;
32
                while (($key = \array_shift($keys))) {
33
                    if ($keys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
34
                        if (!isset($optTree[$key]) || !\is_array($optTree[$key])) {
35
                            $optTree[$key] = [];
36
                        }
37
                        $optTree = &$optTree[$key];
38
                    } else {
39
                        $optTree[$key] = $value;
40
                    }
41
                }
42
            }
43
        }
44
    }
45
46
    /**
47
     * Merge the Array Smart.
48
     *
49
     * @param array $array1
50
     * @param array $array2
51
     *
52
     * @return array
53
     */
54
    public static function mergeRecursiveDistinct(array &$array1, array &$array2)
55
    {
56
        $merged = $array1;
57
58
        foreach ($array2 as $key => &$value) {
59
            if (\is_array($value) && isset($merged[$key]) && \is_array($merged[$key])) {
60
                $merged[$key] = self::mergeRecursiveDistinct($merged[$key], $value);
61
            } else {
62
                $merged[$key] = $value;
63
            }
64
        }
65
66
        return $merged;
67
    }
68
}
69