URL: http://www.ferolen.com/blog/insert-into-add-element-to-array-at-specific-position-php/
Usually, we can search and identify PHP function just by looking at it’s name which denotes what a function does. For example arraypop(), arraypush(), arraysearch(), arrayreverse(), etc. But how about inserting an element into the middle of an array?
arraysplice(array &$input, int $offset [, int $length= 0 [, mixed $replacement]] ) is the function that you are looking for. Format to use this function to add new element at certain position in an array is arraysplice($array, $insertposition, 0, $elementto_insert);. See below for an example of how this function behaves.
<?php // $input is now array("red", "green") // $input is now array("red", "yellow") // $input is now array("red", "orange") // $input is now array("red", "green", "blue", "black", "maroon") // $input is now array("red", "green", "blue", "purple", "yellow"); ?>
You need to login to post a comment.
