array_functions.php ➔ array_depth()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace League\Database\Utils;
4
5
/**
6
 * Determines if an array is associative
7
 * @deprecated and will removed in further MINOR version
8
 *
9
 * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
10
 *
11
 * @param  array  $array
12
 * @return bool
13
 */
14
function array_is_assoc(array $array)
15
{
16
    $keys = array_keys($array);
17
18
    return array_keys($keys) !== $keys;
19
}
20
21
/**
22
 * Get depth of array
23
 * @deprecated and will removed in further MINOR version
24
 *
25
 * @param array $array
26
 *
27
 * @return int
28
 */
29
function array_depth(array $array)
30
{
31
    $maxDepth = 1;
32
33
    foreach ($array as $value) {
34
        if (is_array($value)) {
35
            $depth = array_depth($value) + 1;
36
37
            if ($depth > $maxDepth) {
38
                $maxDepth = $depth;
39
            }
40
        }
41
    }
42
43
    return $maxDepth;
44
}
45
46
/**
47
 * Flatten a multi-dimensional array into a single level
48
 * @deprecated and will removed in further MINOR version
49
 *
50
 * @param  array  $array
51
 * @param  int  $depth
52
 * @return array
53
 */
54
function array_flatten(array $array, $depth = INF)
55
{
56
    return array_reduce($array, function ($result, $item) use ($depth) {
57
        if (!is_array($item)) {
58
            return array_merge($result, [$item]);
59
        } elseif ($depth === 1) {
60
            return array_merge($result, array_values($item));
61
        } else {
62
            return array_merge($result, array_flatten($item, $depth - 1));
63
        }
64
    }, []);
65
}
66
67
/**
68
 * Flatten a multi-dimensional array into a single level with saving keys
69
 * @deprecated and will removed in further MINOR version
70
 *
71
 * @param  array  $array
72
 * @return array
73
 */
74
function array_flatten_assoc(array $array)
75
{
76
    $result = [];
77
78
    foreach ($array as $key => $value) {
79
        if (is_array($value)) {
80
            $result = array_flatten_assoc($value) + $result;
81
        } else {
82
            $result[$key] = $value;
83
        }
84
    }
85
86
    return $result;
87
}
88
89
/**
90
 * Check if array contains from specific types
91
 * @deprecated and will removed in further MINOR version
92
 *
93
 * @param array  $array
94
 * @param string $type
95
 *
96
 * @return bool
97
 */
98
function is_array_of_type(array $array, string $type) : bool
99
{
100
    $types = [
101
        'boolean',
102
        'integer',
103
        'double',
104
        'string',
105
        'array',
106
        'object',
107
        'resource',
108
        'NULL',
109
    ];
110
111
    if (!in_array($type, $types)) {
112
        return false;
113
    }
114
115
    foreach ($array as $item) {
116
        if (gettype($item) !== $type) {
117
            return false;
118
        }
119
    }
120
121
    return true;
122
}
123