Skip to content

Instantly share code, notes, and snippets.

@oranblackwell
Last active December 31, 2015 05:49
Show Gist options
  • Save oranblackwell/7943057 to your computer and use it in GitHub Desktop.
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.
<?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