ArrayHelpers::indexedValuesToAssocKeys()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 12
rs 10
cc 3
nc 1
nop 1
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
        _deprecated_function(__METHOD__, '1.4.0');
19
        // Keys of the array
20
        $keys = array_keys($array);
21
22
        // If the array keys of the keys match the keys, then the array must
23
        // not be associative (e.g. the keys array looked like {0:0, 1:1...}).
24
        return array_keys($keys) !== $keys;
25
    }
26
27
    /**
28
     * Converts indexed values to associative keys.
29
     *
30
     * @since 0.1.0
31
     *
32
     * @param array $array The array to convert.
33
     *
34
     * @return array
35
     */
36
    public static function indexedValuesToAssocKeys(array $array)
37
    {
38
        _deprecated_function(__METHOD__, '1.4.1');
39
        $values = array_map(function ($value) {
40
            return is_array($value) ? $value : [];
41
        }, $array);
42
43
        $keys = array_map(function ($key) use ($array) {
44
            return is_int($key) ? $array[$key] : $key;
45
        }, array_keys($array));
46
47
        return array_combine($keys, $values);
48
    }
49
}
50