-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArr.php
157 lines (138 loc) · 3.46 KB
/
Arr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Colibri\Util;
use Colibri\Pattern\Helper;
/**
* Array manipulation helper.
*/
class Arr extends Helper
{
/**
* Overwrites values of the $array with values from $with array.
*
* @param array $array
* @param array $with
*
* @return array
*/
public static function overwrite(array $array, array $with)
{
return array_replace_recursive($array, $with);
}
/**
* Gets value from $array using "dot" notation, or returns $default value.
*
* @param array $array
* @param string $dottedKey
* @param mixed $default
*
* @return mixed
*/
public static function get(array $array, string $dottedKey = null, $default = null)
{
if ($dottedKey === null) {
return $array;
}
if (isset($array[$dottedKey])) {
return $array[$dottedKey];
}
foreach (explode('.', $dottedKey) as $segment) {
if ( ! is_array($array) || ! array_key_exists($segment, $array)) {
return $default;
}
$array = $array[$segment];
}
return $array;
}
/**
* Sets $value to $array using "dot" notation.
*
* @param array $array
* @param string $dottedKey
* @param mixed $value
*
* @return mixed
*/
public static function &set(array &$array, string $dottedKey, $value)
{
if ($dottedKey === null) {
return $array = $value;
}
if (isset($array[$dottedKey])) {
$array[$dottedKey] = $value;
return $array;
}
$keyParts = explode('.', $dottedKey, 2);
if ( ! isset($array[$keyParts[0]])) {
$array[$keyParts[0]] = [];
}
$array[$keyParts[0]] = count($keyParts) === 2
? self::set($array[$keyParts[0]], $keyParts[1], $value)
: $value;
return $array;
}
/**
* Removes value from $array.
*
* @param array $array
* @param string $dottedKey
*
* @return mixed|null returns removed value or null if key not found
*/
public static function remove(array &$array, string $dottedKey)
{
if (isset($array[$dottedKey])) {
$value = $array[$dottedKey];
unset($array[$dottedKey]);
return $value;
}
$k = explode('.', $dottedKey, 2);
return isset($k[1])
? self::remove($array[$k[0]], $k[1])
: null;
}
/**
* Returns only specified by $keys values from $array.
*
* @param array $array
* @param array $keys
*
* @return array
*/
public static function only(array $array, array $keys)
{
return array_intersect_key($array, array_flip($keys));
}
/**
* in_array() alias.
*
* @param array $array
* @param mixed $value
*
* @return bool
*/
public static function contains(array $array, $value): bool
{
return in_array($value, $array);
}
/**
* @param array $array
* @param int $count
*
* @return array
*/
public static function last(array $array, int $count = 1): array
{
return array_slice($array, -$count);
}
/**
* Transpose. Flips diagonally.
*
* @param array $array
*
* @return array
*/
public static function transpose(array $array): array
{
return array_map(null, ...$array);
}
}