Last active
December 31, 2015 05:49
-
-
Save oranblackwell/7943057 to your computer and use it in GitHub Desktop.
Replace value within an array with multiple new values while keeping the numerical order
AKA insert/replace into the middle of array.
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 | |
/** | |
* replace value within an array with multiple new values while keeping the numerical order | |
* AKA insert/replace into the middle of array | |
* | |
* @param $parentArray | |
* @param int $numericalKey the key of the element to be removed | |
*/ | |
$parentArray = array(0,1,2,3,4,5,6,7,8,9); | |
$numericalKey = 3; | |
array_splice($parentArray, $numericalKey, count($parentArray), | |
array_merge(array('values', 'to', 'be', 'inserted'), | |
array_slice($parentArray, $numericalKey+1))); | |
print_r($parentArray); | |
// outputs Array ( [0] => 0 [1] => 1 [2] => 2 [3] => values [4] => to [5] => be [6] => inserted [7] => 4 [8] => 5 [9] => 6 [10] => 7 [11] => 8 [12] => 9 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment