Hi
I got the folllowing array:
$arr = {4, 9, 10, 3}
I would like to push the value 2 into the array so it would be the new 3rd element
The new final array will be:
$arr = {4, 9, 2, 10, 3}
Is there a simple PHP function that lets you dynamically add elements into a specific index in an array
and automatically shifts the trailing values?
I know that i can implement it myself, but I am looking for a built-in efficient method to do it
regards
how to push an element into the middle of an array
Moderator: General Moderators
From PHP Manual
anton at titov dot net
22-Jan-2003 10:23
For the note above: you can use array_splice function to insert a new value (or array) in the middle of array, this will work like your code does:
<?php
$list = array(
"0" => "zero",
"1" => "one",
"2" => "two",
"3" => "three",
"4" => "four",
"5" => "five",
"6" => "six"
);
$value = "New Number Three";
$key = "3";
$new = array_splice($list, $key, 0, $value);
?>