PluginProbe
Customify / 2.5.7
Customify v2.5.7
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / includes / lib / class-customify-array.php

class-customify-array.php in Customify 2.5.7, at includes/lib/class-customify-array.php

337 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This is a utility class that groups all our array related helper functions.
4 *
5 * @see https://pixelgrade.com
6 * @author Pixelgrade
7 * @version 1.0.0
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly
12 }
13
14 if ( ! class_exists( 'Customify_Array' ) ) {
15
16 class Customify_Array {
17 /**
18 * Insert a value or key/value pair before a specific key in an array. If key doesn't exist, value is prepended
19 * at the beginning of the array.
20 *
21 * @param array $array
22 * @param string $key
23 * @param mixed $insert
24 *
25 * @return array
26 */
27 public static function insertBeforeKey( $array, $key, $insert ) {
28 $keys = array_keys( $array );
29 $index = array_search( $key, $keys );
30 $pos = ( ( false === $index ) ? 0 : $index );
31 if ( ! is_array( $insert ) ) {
32 $insert = array( $insert );
33 }
34
35 return array_merge( array_slice( $array, 0, $pos ), $insert, array_slice( $array, $pos ) );
36 }
37
38 /**
39 * Insert a value (array included) or key/value pair after a specific key in an array. If key doesn't exist, value is appended
40 * to the end of the array.
41 *
42 * @param array $array
43 * @param string $key
44 * @param mixed $insert
45 *
46 * @return array
47 */
48 public static function insertAfterKey( $array, $key, $insert ) {
49 $keys = array_keys( $array );
50 $index = array_search( $key, $keys );
51 $pos = ( ( false === $index ) ? count( $array ) : $index + 1 );
52 if ( ! is_array( $insert ) ) {
53 $insert = array( $insert );
54 }
55
56 return array_merge( array_slice( $array, 0, $pos ), $insert, array_slice( $array, $pos ) );
57 }
58
59 /**
60 * Find a subarray that has the desired key=>value. We will only go one level deep.
61 *
62 * For example, given the following array:
63 * array( array( 'two' => 'value1' ), array( 'two' => 'value2' ) )
64 * you can search for the key of the subarray containing the 'two' key with the 'value2', that is 1
65 *
66 * @param array $array The array in which to search
67 * @param string $key The key to search for
68 * @param mixed $value The value to search for
69 *
70 * @return mixed|false
71 */
72 public static function findSubarrayByKeyValue( $array, $key, $value ) {
73 // Bail if it's not array
74 if ( ! is_array( $array ) ) {
75 return false;
76 }
77
78 foreach ( $array as $k => $v ) {
79 if ( isset( $v[ $key ] ) && $value == $v[ $key ] ) {
80 return $k;
81 }
82 }
83
84 return false;
85 }
86
87 /**
88 * Search an array of objects for a certain property value and return the index where it was found.
89 *
90 * @param array $array
91 * @param string $property
92 * @param mixed $value
93 *
94 * @return int|string|false
95 */
96 public static function objArraySearch( $array, $property, $value ) {
97 foreach ( $array as $key => $array_inf ) {
98 if ( property_exists( $array_inf, $property ) && $array_inf->{$property} == $value ) {
99 return $key;
100 }
101 }
102
103 return false;
104 }
105
106 /**
107 * Get the difference between two associative arrays, recursively.
108 *
109 * @link http://be2.php.net/manual/en/function.array-diff-assoc.php#114297
110 *
111 * @param array $array1
112 * @param array $array2
113 *
114 * @return bool|array
115 */
116 public static function arrayDiffAssocRecursive( $array1, $array2 ) {
117 foreach ( $array1 as $key => $value ) {
118 if ( is_array( $value ) ) {
119 if ( ! isset( $array2[ $key ] ) ) {
120 $difference[ $key ] = $value;
121 } elseif ( ! is_array( $array2[ $key ] ) ) {
122 $difference[ $key ] = $value;
123 } else {
124 $new_diff = self::arrayDiffAssocRecursive( $value, $array2[ $key ] );
125 if ( false !== $new_diff ) {
126 $difference[ $key ] = $new_diff;
127 }
128 }
129 } elseif ( ! array_key_exists( $key, $array2 ) || $array2[ $key ] != $value ) {
130 $difference[ $key ] = $value;
131 }
132 }
133
134 return ! isset( $difference ) ? false : $difference;
135 }
136
137 /**
138 * Searches for an array entry that partially matches the needle and returns the first found key
139 *
140 * @param string $needle
141 * @param array $haystack
142 *
143 * @return bool|int|string The first key whose value matched the partial needle. False on failure or invalid input.
144 */
145 public static function strArraySearch( $needle, $haystack ) {
146 if ( empty( $haystack ) ) {
147 return false;
148 }
149
150 if ( ! is_array( $haystack ) ) {
151 return false;
152 }
153
154 foreach ( $haystack as $key => $value ) {
155 if ( ! is_string( $value ) ) {
156 return false;
157 }
158
159 if ( false !== strpos( $value, $needle ) ) {
160 return $key;
161 }
162 }
163
164 return false;
165 }
166
167 /**
168 * Searches in reverse order for an array entry that partially matches the needle and returns the first found key
169 *
170 * @param string $needle
171 * @param array $haystack
172 *
173 * @return bool|int|string The first key whose value matched the partial needle. False on failure or invalid input.
174 */
175 public static function strrArraySearch( $needle, $haystack ) {
176 if ( empty( $haystack ) ) {
177 return false;
178 }
179
180 if ( ! is_array( $haystack ) ) {
181 return false;
182 }
183
184 $haystack = array_reverse( $haystack, true );
185
186 foreach ( $haystack as $key => $value ) {
187 if ( ! is_string( $value ) ) {
188 return false;
189 }
190
191 if ( false !== strpos( $value, $needle ) ) {
192 return $key;
193 }
194 }
195
196 return false;
197 }
198
199 /**
200 * Detaches a specified item from an array and returns that item.
201 *
202 * @param array $array The array from which you want to detach an item (by reference).
203 * @param mixed $key The key to detach and return.
204 *
205 * @return mixed|false Returns the key that was detached, or false if no key was found.
206 */
207 public static function detach( array &$array, $key ) {
208 if ( ! array_key_exists( $key, $array ) ) {
209 return false;
210 }
211 $value = $array[ $key ];
212 unset( $array[ $key ] );
213
214 return $value;
215 }
216
217 /**
218 * Detaches a specified item from an array by value and returns that item.
219 *
220 * @param array $array The array from which you want to detach an item (by reference).
221 * @param mixed $value The value to find, detach, and return.
222 *
223 * @return mixed|false
224 */
225 public static function detach_by_value( array &$array, $value ) {
226 $key = array_search( $value, $array );
227 if ( ! $key ) {
228 return false;
229 }
230
231 return self::detach( $array, $key );
232 }
233
234 /**
235 * Moves an item from one position in an array to another position in the array.
236 *
237 * @param $array
238 * @param $old_index
239 * @param $new_index
240 *
241 * @return mixed
242 */
243 public static function reorder( $array, $old_index, $new_index ) {
244 array_splice(
245 $array,
246 $new_index,
247 count( $array ),
248 array_merge(
249 array_splice( $array, $old_index, 1 ),
250 array_slice( $array, $new_index, count( $array ) )
251 )
252 );
253
254 return $array;
255 }
256
257 /**
258 * Marge arrays recursively and distinct
259 *
260 * Merges any number of arrays / parameters recursively, replacing
261 * entries with string keys with values from latter arrays.
262 * If the entry or the next value to be assigned is an array, then it
263 * automagically treats both arguments as an array.
264 * Numeric entries are appended, not replaced, but only if they are
265 * unique
266 *
267 * @param array $base Initial array to merge.
268 * @param array ... Variable list of arrays to recursively merge.
269 *
270 * @return array
271 *
272 * @link http://www.php.net/manual/en/function.array-merge-recursive.php#96201
273 * @author Mark Roduner <mark.roduner@gmail.com>
274 */
275 public static function array_merge_recursive_distinct() {
276 $arrays = func_get_args();
277 $base = array_shift( $arrays );
278 if ( ! is_array( $base ) ) {
279 $base = empty( $base ) ? array() : array( $base );
280 }
281 foreach ( $arrays as $append ) {
282 if ( ! is_array( $append ) ) {
283 $append = array( $append );
284 }
285 foreach ( $append as $key => $value ) {
286 if ( ! array_key_exists( $key, $base ) && ! is_numeric( $key ) ) {
287 $base[ $key ] = $append[ $key ];
288 continue;
289 }
290 if ( is_array( $value ) || ( array_key_exists( $key, $base ) && is_array( $base[ $key ] ) ) ) {
291 if ( ! isset( $base[ $key ] ) ) {
292 $base[ $key ] = array();
293 }
294 $base[ $key ] = self::array_merge_recursive_distinct( $base[ $key ], $append[ $key ] );
295 } else if ( is_numeric( $key ) ) {
296 if ( ! in_array( $value, $base ) ) {
297 $base[] = $value;
298 }
299 } else {
300 $base[ $key ] = $value;
301 }
302 }
303 }
304
305 return $base;
306 }
307
308 /**
309 * Orders an associative array by several keys values, each with its own order.
310 *
311 * See array_multisort() for flags.
312 *
313 * Taken from here: http://docs.php.net/manual/en/function.array-multisort.php#100534
314 *
315 * @return array
316 */
317 public static function array_orderby() {
318 $args = func_get_args();
319 $data = array_shift( $args );
320 foreach ( $args as $n => $field ) {
321 if ( is_string( $field ) ) {
322 $tmp = array();
323 foreach ( $data as $key => $row ) {
324 $tmp[ $key ] = $row[ $field ];
325 }
326 $args[ $n ] = $tmp;
327 }
328 }
329 $args[] = &$data;
330 call_user_func_array( 'array_multisort', $args );
331
332 return array_pop( $args );
333 }
334 }
335
336 }
337