| @@ -10,9 +10,9 @@ | ||
| 10 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | 11 | exit; // Exit if accessed directly |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | -if ( ! class_exists( 'Customify_Array' ) ) { | |
| 14 | +if ( ! class_exists( 'Customify_Array' ) ) : | |
| 15 | 15 | |
| 16 | 16 | class Customify_Array { |
| 17 | 17 | /** |
| 18 | 18 | * Insert a value or key/value pair before a specific key in an array. If key doesn't exist, value is prepended |
| @@ -30,9 +30,8 @@ | ||
| 30 | 30 | $pos = ( ( false === $index ) ? 0 : $index ); |
| 31 | 31 | if ( ! is_array( $insert ) ) { |
| 32 | 32 | $insert = array( $insert ); |
| 33 | 33 | } |
| 34 | - | |
| 35 | 34 | return array_merge( array_slice( $array, 0, $pos ), $insert, array_slice( $array, $pos ) ); |
| 36 | 35 | } |
| 37 | 36 | |
| 38 | 37 | /** |
| @@ -51,9 +50,8 @@ | ||
| 51 | 50 | $pos = ( ( false === $index ) ? count( $array ) : $index + 1 ); |
| 52 | 51 | if ( ! is_array( $insert ) ) { |
| 53 | 52 | $insert = array( $insert ); |
| 54 | 53 | } |
| 55 | - | |
| 56 | 54 | return array_merge( array_slice( $array, 0, $pos ), $insert, array_slice( $array, $pos ) ); |
| 57 | 55 | } |
| 58 | 56 | |
| 59 | 57 | /** |
| @@ -63,9 +61,9 @@ | ||
| 63 | 61 | * array( array( 'two' => 'value1' ), array( 'two' => 'value2' ) ) |
| 64 | 62 | * you can search for the key of the subarray containing the 'two' key with the 'value2', that is 1 |
| 65 | 63 | * |
| 66 | 64 | * @param array $array The array in which to search |
| 67 | - * @param string $key The key to search for | |
| 65 | + * @param string $key The key to search for | |
| 68 | 66 | * @param mixed $value The value to search for |
| 69 | 67 | * |
| 70 | 68 | * @return mixed|false |
| 71 | 69 | */ |
| @@ -98,9 +96,8 @@ | ||
| 98 | 96 | if ( property_exists( $array_inf, $property ) && $array_inf->{$property} == $value ) { |
| 99 | 97 | return $key; |
| 100 | 98 | } |
| 101 | 99 | } |
| 102 | - | |
| 103 | 100 | return false; |
| 104 | 101 | } |
| 105 | 102 | |
| 106 | 103 | /** |
| @@ -209,9 +206,8 @@ | ||
| 209 | 206 | return false; |
| 210 | 207 | } |
| 211 | 208 | $value = $array[ $key ]; |
| 212 | 209 | unset( $array[ $key ] ); |
| 213 | - | |
| 214 | 210 | return $value; |
| 215 | 211 | } |
| 216 | 212 | |
| 217 | 213 | /** |
| @@ -226,9 +222,8 @@ | ||
| 226 | 222 | $key = array_search( $value, $array ); |
| 227 | 223 | if ( ! $key ) { |
| 228 | 224 | return false; |
| 229 | 225 | } |
| 230 | - | |
| 231 | 226 | return self::detach( $array, $key ); |
| 232 | 227 | } |
| 233 | 228 | |
| 234 | 229 | /** |
| @@ -249,61 +244,50 @@ | ||
| 249 | 244 | array_splice( $array, $old_index, 1 ), |
| 250 | 245 | array_slice( $array, $new_index, count( $array ) ) |
| 251 | 246 | ) |
| 252 | 247 | ); |
| 253 | - | |
| 254 | 248 | return $array; |
| 255 | 249 | } |
| 256 | 250 | |
| 257 | 251 | /** |
| 258 | - * Marge arrays recursively and distinct | |
| 252 | + * Great answer from here: http://be2.php.net/manual/en/function.array-merge-recursive.php#92195 | |
| 259 | 253 | * |
| 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 | |
| 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): | |
| 266 | 258 | * |
| 267 | - * @param array $base Initial array to merge. | |
| 268 | - * @param array ... Variable list of arrays to recursively merge. | |
| 259 | + * array_merge_recursive(array('key' => 'org value'), array('key' => 'new value')); | |
| 260 | + * => array('key' => array('org value', 'new value')); | |
| 269 | 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 | |
| 270 | 274 | * @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> | |
| 275 | + * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk> | |
| 276 | + * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com> | |
| 274 | 277 | */ |
| 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 ); | |
| 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; | |
| 284 | 286 | } |
| 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 | 287 | } |
| 304 | 288 | |
| 305 | - return $base; | |
| 289 | + return $merged; | |
| 306 | 290 | } |
| 307 | 291 | |
| 308 | 292 | /** |
| 309 | 293 | * Orders an associative array by several keys values, each with its own order. |
| @@ -331,5 +315,6 @@ | ||
| 331 | 315 | |
| 332 | 316 | return array_pop( $args ); |
| 333 | 317 | } |
| 334 | 318 | } |
| 335 | -} | |
| 319 | + | |
| 320 | +endif; | |