PROBLEM :
How can we insert a new item into an array on any position, say for example in the middle of array?
or
How can we insert a new item into an array on any position?
php arrays insert
SOLUTION 1:
It only requires one function call to array_splice:
Php Code
$original = array( ‘w', ‘i', ‘k', ‘e', ‘t' ); $inserted = array( 'x' ); // Not necessarily an array array_splice( $original, 3, 0, $inserted ); // splice in at position 3 // $original is now w i k x e t
SOLUTION 2:
Here is the solution for insert arrays:
Php Code
function array_insert(&$array, $value, $index) { return $array = array_merge(array_splice($array, max(0, $index - 1)), array($value), $array); }
SOLUTION 3:
A function that can insert at both integer and string positions:
Php Code
/** * @param array $array * @param int|string $position * @param mixed $insert */ function array_insert(&$array, $position, $insert) { if (is_int($position)) { array_splice($array, $position, 0, $insert); } else { $pos = array_search($position, array_keys($array)); $array = array_merge( array_slice($array, 0, $pos), $insert, array_slice($array, $pos) ); } }
Integer usage:
Php Code
$arr = ["one", "two", "three"]; array_insert( $arr, 1, "one-half" ); // -> array ( 0 => 'one', 1 => 'one-half', 2 => 'two', 3 => 'three', )
String Usage:
Php Code
$arr = [ "name" => [ "type" => "string", "maxlength" => "40", ], "email" => [ "type" => "email", "maxlength" => "160", ], ]; array_insert( $arr, "email", [ "phone" => [ "type" => "string", "format" => "phone", ], ] );
Php Code
// -> array ( 'name' => array ( 'type' => 'string', 'maxlength' => '40', ), 'phone' => array ( 'type' => 'string', 'format' => 'phone', ), 'email' => array ( 'type' => 'email', 'maxlength' => '160', ), )
[ad type=”banner”]
SOLUTION 4:
We can use this:
Php Code
foreach ($array as $key => $value) { if($key==1) { $new_array[]=$other_array; } $new_array[]=$value; }
SOLUTION 5:
Here is the another Solution: –
PHP Code:
Php Code
<?php $original = array( '1','2','3','4','5' ); echo 'Original array : '."\n"; foreach ($original as $x) {echo "$x ";} $inserted = '$'; array_splice( $original, 3, 0, $inserted ); echo " \n After inserting '$' the array is : "."\n"; foreach ($original as $x) {echo "$x ";} echo "\n" ?>
Sample Output:
Original array :
1 2 3 4 5
After inserting ‘$’ the array is :
1 2 3 $ 4 5
SOLUTION 6:
splice method can be used for adding and/or removing elements from an array.
The first argument specifies the location at which to begin adding or removing elements.
The second argument specifies the number of elements to delete.
When using splice to add elements to an array, the second argument would be zero.
The third and subsequent arguments are elements to be added to the array.
Php Code
var ar = [1, 2, 3, 4, 5, 6]; // arguments: start position, number of elements to delete, elements to add ar.splice(3, 0, ‘d', ‘e', ‘f'); console.log( ar ); // [1, 2, 3, “d", “e", “f", 4, 5, 6]
[ad type=”banner”]
SOLUTION 7:
Hint for adding an element at the beginning of an array:
Php Code
$a = array('first', 'second'); $a[-1] = 'i am the new first element';
then:
Php Code
foreach($a as $aelem) echo $a . ' '; //returns first, second, i am...
but:
Php Code
for ($i = -1; $i < count($a)-1; $i++) echo $a . ' '; //returns i am as 1st element
SOLUTION 8:
Add Elements to the Beginning of an Array:
unshift method is used to add elements to the beginning of an array.
It accepts multiple arguments, adjusts the indexes of existing elements, and returns the new length of the array.
The unshift method modifies the array on which it is invoked.
First we invoke unshift passing a single argument, then multiple arguments, displaying the results using console.log:
Php Code
var ar = ['one', 'two', 'three']; // add single element ar.unshift('zero'); console.log( ar ); // ["zero", "one", "two", "three"] // add multiple elements ar.unshift(0, 1, 2, 3); console.log( ar ); // [0, 1, 2, 3, "zero", "one", "two", "three"]
SOLUTION 9:
Here is simple function for insert new element after a specific key, while preserving integer keys:
Php Code
private function arrayInsertAfterKey($array, $afterKey, $key, $value){ $pos = array_search($afterKey, array_keys($array)); return array_merge( array_slice($array, 0, $pos, $preserve_keys = true), array($key=>$value), array_slice($array, $pos, $preserve_keys = true) ); }
SOLUTION 10:
Normally, with scalar values:
Php Code
$elements = array('foo', ...); array_splice($array, $position, $length, $elements);
To insert a single array element into our array don’t forget to wrap the array in an array (as it was a scalar value!):
Php Code
$element = array('key1'=>'value1'); $elements = array($element); array_splice($array, $position, $length, $elements);
[ad type=”banner”]