ArrayHelpers::indexedValuesToAssocKeys()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Utils;
4
5
class ArrayHelpers
6
{
7
    /**
8
     * Checks if an array is associative.
9
     *
10
     * @since 0.1.0
11
     *
12
     * @param array $array The array to check.
13
     *
14
     * @return boolean
15
     */
16
    public static function isAssoc(array $array)
17
    {
18
        // Keys of the array
19
        $keys = array_keys($array);
20
21
        // If the array keys of the keys match the keys, then the array must
22
        // not be associative (e.g. the keys array looked like {0:0, 1:1...}).
23
        return array_keys($keys) !== $keys;
24
    }
25
26
    /**
27
     * Converts indexed values to associative keys.
28
     *
29
     * @since 0.1.0
30
     *
31
     * @param array $array The array to convert.
32
     *
33
     * @return array
34
     */
35
    public static function indexedValuesToAssocKeys(array $array)
36
    {
37
        $values = array_map(function ($value) {
38
            return is_array($value) ? $value : [];
39
        }, $array);
40
41
        $keys = array_map(function ($key) use ($array) {
42
            return is_int($key) ? $array[$key] : $key;
43
        }, array_keys($array));
44
45
        return array_combine($keys, $values);
46
    }
47
}
48