-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arr.php
210 lines (177 loc) · 4.51 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace MetaBox\Support;
class Arr {
/**
* New array map function that accepts more params than just values.
* Params: array|item, callback, other params.
*/
public static function map() {
$args = func_get_args();
$items = array_shift( $args );
$callback = array_shift( $args );
if ( ! is_array( $items ) ) {
array_unshift( $args, $items );
return call_user_func_array( $callback, $args );
}
return array_map(
function( $item ) use ( $callback, $args ) {
array_unshift( $args, $item );
return call_user_func_array( $callback, $args );
},
$items
);
}
/**
* Convert a comma separated string to array.
*
* @param array|string $csv Comma separated string.
*/
public static function from_csv( $csv ) : array {
return is_array( $csv ) ? $csv : array_filter( array_map( 'trim', explode( ',', $csv . ',' ) ) );
}
/**
* Change array key.
*
* @param array $array Input array.
* @param string $from From key.
* @param string $to To key.
*/
public static function change_key( &$array, $from, $to ) {
if ( isset( $array[ $from ] ) ) {
$array[ $to ] = $array[ $from ];
}
unset( $array[ $from ] );
}
/**
* Ensure a variable is an array.
*/
public static function ensure( $input ) : array {
return (array) $input;
}
/**
* Flatten an array.
* @link https://stackoverflow.com/a/1320156/371240
*/
public static function flatten( array $array ) : array {
$return = [];
array_walk_recursive(
$array,
function( $a ) use ( &$return ) {
$return[] = $a;
}
);
return $return;
}
/**
* Convert flatten collection (with dot notation) to multiple dimensional array
*
* @param array $collection Collection to be flatten.
* @return array
*/
public static function unflatten( $collection ) {
$collection = (array) $collection;
$output = [];
foreach ( $collection as $key => $value ) {
self::set( $output, $key, $value );
if ( is_array( $value ) && ! strpos( $key, '.' ) ) {
$nested = self::unflatten( $value );
$output[ $key ] = $nested;
}
}
return $output;
}
/**
* Set array element value with dot notation.
*/
public static function set( &$array, $key, $value ) {
if ( is_null( $key ) ) {
$array = $value;
return $array;
}
// Do not parse email value.
if ( is_email( $key ) ) {
$array[ $key ] = $value;
return;
}
$keys = explode( '.', $key );
while ( count( $keys ) > 1 ) {
$key = array_shift( $keys );
// If the key doesn't exist at this depth, we will just create an empty array
// to hold the next value, allowing us to create the arrays to hold final
// values at the correct depth. Then we'll keep digging into the array.
if ( ! isset( $array[ $key ] ) || ! is_array( $array[ $key ] ) ) {
$array[ $key ] = [];
}
$array =& $array[ $key ];
}
$array[ array_shift( $keys ) ] = $value;
}
/**
* Get array element value with dot notation.
*/
public static function get( $array, $key, $default = null ) {
if ( is_null( $key ) ) {
return $array;
}
$keys = explode( '.', $key );
foreach ( $keys as $key ) {
if ( isset( $array[ $key ] ) ) {
$array = $array[ $key ];
} else {
return $default;
}
}
return $array;
}
public static function to_depth( $input, $depth ) {
$current_depth = is_array( $input ) ? self::depth( $input ) : 0;
if ( $depth < $current_depth ) {
while ( $current_depth > $depth ) {
$input = reset( $input );
$current_depth--;
}
} elseif ( $depth > $current_depth ) {
while ( $current_depth < $depth ) {
$input = [ $input ];
$current_depth++;
}
}
return $input;
}
public static function depth( array $array ) {
$max_depth = 1;
foreach ( $array as $key => $value ) {
if ( !is_string($key) && is_array( $value ) ) {
$depth = self::depth( $value ) + 1;
if ( $depth > $max_depth ) {
$max_depth = $depth;
}
}
}
return $max_depth;
}
public static function remove_first( &$array, $query ) {
$keys = explode( '.', $query );
$key = array_shift( $keys );
if ( count( $keys ) === 0 ) {
if ( is_array( $array ) && array_key_exists( $key, $array ) ) {
unset( $array[ $key ][0] );
}
return;
}
if ( $key === '*' ) {
foreach ( $array as $k => $v ) {
if ( is_array( $array[ $k ] ) ) {
self::remove_first( $array[ $k ], implode( '.', $keys ) );
}
}
return;
}
if ( $key === '' ) {
return;
}
if ( is_array( $array[ $key ] ) ) {
self::remove_first( $array[ $key ], implode( '.', $keys ) );
}
}
}