|
1
|
|
|
<?php |
|
2
|
|
|
namespace Bedd\Common; |
|
3
|
|
|
|
|
4
|
|
|
use Bedd\Common\Traits\StaticClassTrait; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* ArrayUtils |
|
8
|
|
|
*/ |
|
9
|
|
|
class ArrayUtils |
|
10
|
|
|
{ |
|
11
|
|
|
use StaticClassTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Test whether an array contains one or more keys by defined $type |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $input the Type: string|int |
|
17
|
|
|
* @param array $input |
|
18
|
|
|
* @param bool $only |
|
19
|
|
|
* @param bool $allow_empty Should an empty $input return true |
|
20
|
|
|
* @return bool |
|
21
|
|
|
*/ |
|
22
|
|
|
private static function hasKeysByType($type, array $input, $only = false, $allow_empty = false) |
|
23
|
|
|
{ |
|
24
|
|
|
if (!is_array($input)) { |
|
25
|
|
|
return false; |
|
26
|
|
|
} |
|
27
|
|
|
if (!$input) { |
|
|
|
|
|
|
28
|
|
|
return $allow_empty; |
|
29
|
|
|
} |
|
30
|
|
|
$count = count(array_filter(array_keys($input), 'is_'.strtolower($type))); |
|
31
|
|
|
return $only ? $count === count($input) : $count > 0; |
|
32
|
|
|
} |
|
33
|
|
|
/** |
|
34
|
|
|
* Test whether an array contains one or more string keys |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $input |
|
37
|
|
|
* @param bool $only |
|
38
|
|
|
* @param bool $allow_empty Should an empty $input return true |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function hasStringKeys(array $input, $only = false, $allow_empty = false) |
|
42
|
|
|
{ |
|
43
|
|
|
return self::hasKeysByType('string', $input, $only, $allow_empty); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Test whether an array contains one or more integer keys |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $input |
|
50
|
|
|
* @param bool $only |
|
51
|
|
|
* @param bool $allow_empty Should an empty $input return true |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function hasIntegerKeys(array $input, $only = false, $allow_empty = false) |
|
55
|
|
|
{ |
|
56
|
|
|
return self::hasKeysByType('int', $input, $only, $allow_empty); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the value of the first found key-name |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $input |
|
63
|
|
|
* @param string[] $keys |
|
64
|
|
|
* @param mixed $default |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function findValueByKeys(array $input, array $keys, $default = null) |
|
68
|
|
|
{ |
|
69
|
|
|
$return = $default; |
|
70
|
|
|
foreach ($keys as $key) { |
|
71
|
|
|
if (isset($input[$key])) { |
|
72
|
|
|
$return = $input[$key]; |
|
73
|
|
|
break; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Flatten a multi-dimensional aray into a one dimensional array |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $input |
|
83
|
|
|
* @param string $preserve_keys |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function flatten(array $input, $preserve_keys = false) |
|
87
|
|
|
{ |
|
88
|
|
|
$return = []; |
|
89
|
|
|
$awr_func = $preserve_keys ? |
|
90
|
|
|
function ($v, $k) use (&$return) {$return[$k] = $v;} : |
|
|
|
|
|
|
91
|
|
|
function ($v) use (&$return) {$return[] = $v;} |
|
|
|
|
|
|
92
|
|
|
; |
|
93
|
|
|
array_walk_recursive($input, $awr_func); |
|
94
|
|
|
return $return; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Rename a key inside a dimensional array |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $input |
|
101
|
|
|
* @param string old_key |
|
102
|
|
|
* @param string new_key |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function renameKey(array $input, $old_key, $new_key) |
|
106
|
|
|
{ |
|
107
|
|
|
$ret = $input; |
|
108
|
|
|
if ($old_key !== $new_key && array_key_exists($old_key, $input)) { |
|
109
|
|
|
$keys = array_keys($input); |
|
110
|
|
|
$offset = array_search($old_key, $keys); |
|
111
|
|
|
$keys[$offset] = $new_key; |
|
112
|
|
|
$ret = array_combine($keys, $input); |
|
113
|
|
|
} |
|
114
|
|
|
return $ret; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.