Last active
September 20, 2016 23:58
-
-
Save codearachnid/4499117 to your computer and use it in GitHub Desktop.
PHP array management has some limits to merging associative arrays. These methods allow for creative positional merging of associative arrays by key. Think of this as array_splice for associative arrays. Another clever option I found recently is http://stackoverflow.com/a/13289272/1542064
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Insert another array into an associative array after the supplied key | |
* | |
* @param string $key | |
* The key of the array you want to pivot around | |
* @param array $source_array | |
* The 'original' source array | |
* @param array $insert_array | |
* The 'new' associative array to merge in by the key | |
* | |
* @return array $modified_array | |
* The resulting merged arrays | |
*/ | |
function array_splice_after_key( $key, $source_array, $insert_array ) { | |
return array_splice_key( $key, $source_array, $insert_array ); | |
} | |
/** | |
* Insert another array into an associative array before the supplied key | |
* | |
* @param string $key | |
* The key of the array you want to pivot around | |
* @param array $source_array | |
* The 'original' source array | |
* @param array $insert_array | |
* The 'new' associative array to merge in by the key | |
* | |
* @return array $modified_array | |
* The resulting merged arrays | |
*/ | |
function array_splice_before_key( $key, $source_array, $insert_array ) { | |
return array_splice_key( $key, $source_array, $insert_array, -1 ); | |
} | |
/** | |
* Insert another array into an associative array around a given key | |
* | |
* @param string $key | |
* The key of the array you want to pivot around | |
* @param array $source_array | |
* The 'original' source array | |
* @param array $insert_array | |
* The 'new' associative array to merge in by the key | |
* @param int $direction | |
* Where to insert the new array relative to given $position by $key | |
* Allowed values: positive or negative numbers - default is 1 (insert after $key) | |
* | |
* @return array $modified_array | |
* The resulting merged arrays | |
*/ | |
function array_splice_key( $key, $source_array, $insert_array, $direction = 1 ){ | |
$position = array_search( $key, array_keys( $source_array ) ) + $direction; | |
// setup the return with the source array | |
$modified_array = $source_array; | |
if( count($source_array) < $position && $position != 0 ) { | |
// push one or more elements onto the end of array | |
array_push( $modified_array, $insert_array ); | |
} else if ( $position < 0 ){ | |
// prepend one or more elements to the beginning of an array | |
array_unshift( $modified_array, $insert_array ); | |
} else { | |
$modified_array = array_slice($source_array, 0, $position, true) + | |
$insert_array + | |
array_slice($source_array, $position, NULL, true); | |
} | |
return $modified_array; | |
} |
Great gist! I found 2 bugs in it while testing it out.
Line 61 should be:
$modified_array = $source_array + $insert_array;
and line 64 should be:
$modified_array = $insert_array + $source_array;
Otherwise the $insert_array is pre/appended as a single sub-array element instead of the array's elements being added individually.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@codearachnid I think for
array_splice_before_key
you would want direction to be0
, not-1
because you want the new element to appear in the "current" position of that key, and to bump that key to a higher position.