Insert New Item in Array on Any Position in PHP

Insert new item in array on any position in PHP

You may find this a little more intuitive. It only requires one function call to array_splice:

$original = array( 'a', 'b', 'c', 'd', 'e' );
$inserted = array( 'x' ); // not necessarily an array, see manual quote

array_splice( $original, 3, 0, $inserted ); // splice in at position 3
// $original is now a b c x d e

If replacement is just one element it is not necessary to put array() around it, unless the element is an array itself, an object or NULL.

RETURN VALUE: To be noted that the function does not return the desired substitution. The $original is passed by reference and edited in place. See the expression array &$array with & in the parameters list .

How to insert an item at the beginning of an array in PHP?

Use array_unshift($array, $item);

$arr = array('item2', 'item3', 'item4');
array_unshift($arr , 'item1');
print_r($arr);

will give you

Array
(
[0] => item1
[1] => item2
[2] => item3
[3] => item4
)

How to insert element into arrays at specific position?

array_slice() can be used to extract parts of the array, and the union array operator (+) can recombine the parts.

$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array)-3, true);

This example:

$array = array(
'zero' => '0',
'one' => '1',
'two' => '2',
'three' => '3',
);
$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array) - 1, true) ;
print_r($res);

gives:


Array
(
[zero] => 0
[one] => 1
[two] => 2
[my_key] => my_value
[three] => 3
)

insert array of new item in any position in another array

You can use array_splice, except that won't keep your keys.

$a1 = array("a"=>"Horse", "b"=>"Dog", "c"=>"Cow");
$a2 = array("d"=>"Cat");

array_splice($a1, 2, 0, $a2);

// $a1 is now: array("a"=>"Horse", "b"=>"Dog", 0=>"Cat", "c"=>"Cow");

If you want Cat to have a key of d, you can use a mix of array_slice and the array union operator (+):

$a1 = array_slice($a1, 0, 2) + $a2 + array_slice($a1, 2);

// $a1 is now: array("a"=>"Horse", "b"=>"Dog", "d"=>"Cat", "c"=>"Cow");

How to push array elemnt to specific position in another array using php

You need to define positions as array and combine it with array_2. Now iterate over this combined array and use code of first reference thread:

<?php

$array_1 = array(1,2,3,4,5);
$array_2 = array('a','b','c');

//define positions array
$positions = array(1,3,4);

//combine position array with $array_2
$positionArray = array_combine($positions, $array_2);

//iterate over combined array
foreach($positionArray as $key => $value){
//use code of first example thread
array_splice($array_1, $key, 0, $value);
//here $key is position where you want to insert
}

print_r($array_1);

Output: https://3v4l.org/Djar2

How to splice an array to insert array at specific position?

array_splice­Docs takes an array of elements to insert. So the call should actually be

array_splice($custom, 1, 0, array($bread_elem));

PHP Insert array to associative array at specific position

$example[] = [
'first' => 'element',
'second' => 'element'];
$example[] = [
'third' => 'element',
'fourth' => 'element'];
$insert[] = [
'insert_first' => 'element',
'insert_second' => 'element'];

$index = 1;
array_splice($example, $index, 0, $insert);
print_r($example);

Gives you :

Array ( 
[0] => Array ( [first] => element [second] => element )
[1] => Array ( [insert_first] => element [insert_second] => element )
[2] => Array ( [third] => element [fourth] => element ) )

Insert array in Database

Iterate over one array using keys also and take values under that key from other arrays:

foreach($type as $key => $t) {
// insert into db values: ($t, $color[$key], $thicknes[$key], $fill[$key] etc)
}


Related Topics



Leave a reply



Submit