PluginProbe
Customify / 1.7.3
Customify v1.7.3
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 / class-customify-array.php

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

321 lines 9.0 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 return array_merge( array_slice( $array, 0, $pos ), $insert, array_slice( $array, $pos ) );
35 }
36
37 /**
38 * Insert a value (array included) or key/value pair after a specific key in an array. If key doesn't exist, value is appended
39 * to the end of the array.
40 *
41 * @param array $array
42 * @param string $key
43 * @param mixed $insert
44 *
45 * @return array
46 */
47 public static function insertAfterKey( $array, $key, $insert ) {
48 $keys = array_keys( $array );
49 $index = array_search( $key, $keys );
50 $pos = ( ( false === $index ) ? count( $array ) : $index + 1 );
51 if ( ! is_array( $insert ) ) {
52 $insert = array( $insert );
53 }
54 return array_merge( array_slice( $array, 0, $pos ), $insert, array_slice( $array, $pos ) );
55 }
56
57 /**
58 * Find a subarray that has the desired key=>value. We will only go one level deep.
59 *
60 * For example, given the following array:
61 * array( array( 'two' => 'value1' ), array( 'two' => 'value2' ) )
62 * you can search for the key of the subarray containing the 'two' key with the 'value2', that is 1
63 *
64 * @param array $array The array in which to search
65 * @param string $key The key to search for
66 * @param mixed $value The value to search for
67 *
68 * @return mixed|false
69 */
70 public static function findSubarrayByKeyValue( $array, $key, $value ) {
71 // Bail if it's not array
72 if ( ! is_array( $array ) ) {
73 return false;
74 }
75
76 foreach ( $array as $k => $v ) {
77 if ( isset( $v[ $key ] ) && $value == $v[ $key ] ) {
78 return $k;
79 }
80 }
81
82 return false;
83 }
84
85 /**
86 * Search an array of objects for a certain property value and return the index where it was found.
87 *
88 * @param array $array
89 * @param string $property
90 * @param mixed $value
91 *
92 * @return int|string|false
93 */
94 public static function objArraySearch( $array, $property, $value ) {
95 foreach ( $array as $key => $array_inf ) {
96 if ( property_exists( $array_inf, $property ) && $array_inf->{$property} == $value ) {
97 return $key;
98 }
99 }
100 return false;
101 }
102
103 /**
104 * Get the difference between two associative arrays, recursively.
105 *
106 * @link http://be2.php.net/manual/en/function.array-diff-assoc.php#114297
107 *
108 * @param array $array1
109 * @param array $array2
110 *
111 * @return bool|array
112 */
113 public static function arrayDiffAssocRecursive( $array1, $array2 ) {
114 foreach ( $array1 as $key => $value ) {
115 if ( is_array( $value ) ) {
116 if ( ! isset( $array2[ $key ] ) ) {
117 $difference[ $key ] = $value;
118 } elseif ( ! is_array( $array2[ $key ] ) ) {
119 $difference[ $key ] = $value;
120 } else {
121 $new_diff = self::arrayDiffAssocRecursive( $value, $array2[ $key ] );
122 if ( false !== $new_diff ) {
123 $difference[ $key ] = $new_diff;
124 }
125 }
126 } elseif ( ! array_key_exists( $key, $array2 ) || $array2[ $key ] != $value ) {
127 $difference[ $key ] = $value;
128 }
129 }
130
131 return ! isset( $difference ) ? false : $difference;
132 }
133
134 /**
135 * Searches for an array entry that partially matches the needle and returns the first found key
136 *
137 * @param string $needle
138 * @param array $haystack
139 *
140 * @return bool|int|string The first key whose value matched the partial needle. False on failure or invalid input.
141 */
142 public static function strArraySearch( $needle, $haystack ) {
143 if ( empty( $haystack ) ) {
144 return false;
145 }
146
147 if ( ! is_array( $haystack ) ) {
148 return false;
149 }
150
151 foreach ( $haystack as $key => $value ) {
152 if ( ! is_string( $value ) ) {
153 return false;
154 }
155
156 if ( false !== strpos( $value, $needle ) ) {
157 return $key;
158 }
159 }
160
161 return false;
162 }
163
164 /**
165 * Searches in reverse order for an array entry that partially matches the needle and returns the first found key
166 *
167 * @param string $needle
168 * @param array $haystack
169 *
170 * @return bool|int|string The first key whose value matched the partial needle. False on failure or invalid input.
171 */
172 public static function strrArraySearch( $needle, $haystack ) {
173 if ( empty( $haystack ) ) {
174 return false;
175 }
176
177 if ( ! is_array( $haystack ) ) {
178 return false;
179 }
180
181 $haystack = array_reverse( $haystack, true );
182
183 foreach ( $haystack as $key => $value ) {
184 if ( ! is_string( $value ) ) {
185 return false;
186 }
187
188 if ( false !== strpos( $value, $needle ) ) {
189 return $key;
190 }
191 }
192
193 return false;
194 }
195
196 /**
197 * Detaches a specified item from an array and returns that item.
198 *
199 * @param array $array The array from which you want to detach an item (by reference).
200 * @param mixed $key The key to detach and return.
201 *
202 * @return mixed|false Returns the key that was detached, or false if no key was found.
203 */
204 public static function detach( array &$array, $key ) {
205 if ( ! array_key_exists( $key, $array ) ) {
206 return false;
207 }
208 $value = $array[ $key ];
209 unset( $array[ $key ] );
210 return $value;
211 }
212
213 /**
214 * Detaches a specified item from an array by value and returns that item.
215 *
216 * @param array $array The array from which you want to detach an item (by reference).
217 * @param mixed $value The value to find, detach, and return.
218 *
219 * @return mixed|false
220 */
221 public static function detach_by_value( array &$array, $value ) {
222 $key = array_search( $value, $array );
223 if ( ! $key ) {
224 return false;
225 }
226 return self::detach( $array, $key );
227 }
228
229 /**
230 * Moves an item from one position in an array to another position in the array.
231 *
232 * @param $array
233 * @param $old_index
234 * @param $new_index
235 *
236 * @return mixed
237 */
238 public static function reorder( $array, $old_index, $new_index ) {
239 array_splice(
240 $array,
241 $new_index,
242 count( $array ),
243 array_merge(
244 array_splice( $array, $old_index, 1 ),
245 array_slice( $array, $new_index, count( $array ) )
246 )
247 );
248 return $array;
249 }
250
251 /**
252 * Great answer from here: http://be2.php.net/manual/en/function.array-merge-recursive.php#92195
253 *
254 * array_merge_recursive does indeed merge arrays, but it converts values with duplicate
255 * keys to arrays rather than overwriting the value in the first array with the duplicate
256 * value in the second array, as array_merge does. I.e., with array_merge_recursive,
257 * this happens (documented behavior):
258 *
259 * array_merge_recursive(array('key' => 'org value'), array('key' => 'new value'));
260 * => array('key' => array('org value', 'new value'));
261 *
262 * array_merge_recursive_distinct does not change the datatypes of the values in the arrays.
263 * Matching keys' values in the second array overwrite those in the first array, as is the
264 * case with array_merge, i.e.:
265 *
266 * array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value'));
267 * => array('key' => array('new value'));
268 *
269 * Parameters are passed by reference, though only for performance reasons. They're not
270 * altered by this function.
271 *
272 * @param array $array1
273 * @param array $array2
274 * @return array
275 * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
276 * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
277 */
278 public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
279 $merged = $array1;
280
281 foreach ( $array2 as $key => &$value ) {
282 if ( is_array( $value ) && isset( $merged [ $key ] ) && is_array( $merged [ $key ] ) ) {
283 $merged [ $key ] = self::array_merge_recursive_distinct( $merged [ $key ], $value );
284 } else {
285 $merged [ $key ] = $value;
286 }
287 }
288
289 return $merged;
290 }
291
292 /**
293 * Orders an associative array by several keys values, each with its own order.
294 *
295 * See array_multisort() for flags.
296 *
297 * Taken from here: http://docs.php.net/manual/en/function.array-multisort.php#100534
298 *
299 * @return array
300 */
301 public static function array_orderby() {
302 $args = func_get_args();
303 $data = array_shift( $args );
304 foreach ( $args as $n => $field ) {
305 if ( is_string( $field ) ) {
306 $tmp = array();
307 foreach ( $data as $key => $row ) {
308 $tmp[ $key ] = $row[ $field ];
309 }
310 $args[ $n ] = $tmp;
311 }
312 }
313 $args[] = &$data;
314 call_user_func_array( 'array_multisort', $args );
315
316 return array_pop( $args );
317 }
318 }
319
320 endif;
321