Completed
Push — master ( 237036...650d4b )
by Dominik
02:47
created

ArrayHelpers::objectToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Utils;
4
5
class ArrayHelpers
6
{
7
    public static function isAssoc(array $array)
8
    {
9
        // Keys of the array
10
        $keys = array_keys($array);
11
12
        // If the array keys of the keys match the keys, then the array must
13
        // not be associative (e.g. the keys array looked like {0:0, 1:1...}).
14
        return array_keys($keys) !== $keys;
15
    }
16
17
    // only converts first dimension of object
18
    public static function objectToArray($obj)
19
    {
20
        return array_map(function ($val) {
21
            return (array) $val;
22
        }, $obj);
23
    }
24
25
    public static function indexedValuesToAssocKeys(array $array)
26
    {
27
        $values = array_map(function ($value) {
28
            return is_array($value) ? $value : [];
29
        }, $array);
30
31
        $keys = array_map(function ($key) use ($array) {
32
            return is_int($key) ? $array[$key] : $key;
33
        }, array_keys($array));
34
35
        return array_combine($keys, $values);
36
    }
37
}
38