ArrayHelpers   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isAssoc() 0 9 1
A indexedValuesToAssocKeys() 0 12 3
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