web
web

Reputation: 2973

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

I know how to insert it to the end by:

$arr[] = $item;

But how to insert it to the beginning?

Upvotes: 294

Views: 295857

Answers (9)

Mark
Mark

Reputation: 98

Have a look at the array_replace_recursive. It will keep indexes to unlike array_merge_recursive.

Upvotes: 0

nito
nito

Reputation: 1313

Since PHP7.4 you can use the spread opperator, very similar to JavaScript

$arr = [$item, ...$arr];

https://wiki.php.net/rfc/spread_operator_for_array

A few more examples:

// Add an item before
$arr = [$item, ...$arr];

// Add an item after
$arr = [...$arr, $item];

// Add a few more
$arr = [$item1, $item2, ...$arr, $item3];

// Multiple arrays and and items
$arr = [$item1, ...$arr1, $item2, ...$arr2, $item3];
  • Note: this is only possible with int keys, not for associative arrays.
  • Note2: The keys are not kept.

BREAKING Example:

$car = ['brand' => 'Audi', 'model' => 'A8'];
$engine = ['cylinder' => 6, 'displacement' => '3000cc'];

$merged = [...$car, ...$engine];

var_dump($merged);

Will result in:

[ Error ] Cannot unpack array with string keys

Upvotes: 27

Vinit Kadkol
Vinit Kadkol

Reputation: 1285

Use array_unshift() to insert the first element in an array.

Use array_shift() to remove the first element of an array.

Upvotes: 11

Farid shahidi
Farid shahidi

Reputation: 352

There are two solutions:

  1. if you have array with keys that matter to you
$new = ['RLG' => array(1,2,3)];
$array = ['s' => array(2,3), 'l' => [], 'o' => [], 'e' => []];

$arrayWithNewAddedItem = array_merge($new, $array);
  1. if you have an array without any keys.
array_unshift(array, value1);

Upvotes: 7

Manish Dhruw
Manish Dhruw

Reputation: 803

Insert an item in the beginning of an associative array with string/custom key

<?php

$array = ['keyOne'=>'valueOne', 'keyTwo'=>'valueTwo'];

$array = array_reverse($array);

$array['newKey'] = 'newValue';

$array = array_reverse($array);

RESULT

[
  'newKey' => 'newValue',
  'keyOne' => 'valueOne',
  'keyTwo' => 'valueTwo'
]

Upvotes: 14

pictoru
pictoru

Reputation: 752

For an associative array you can just use merge.

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

Upvotes: 14

Arnas Pečelis
Arnas Pečelis

Reputation: 1008

Or you can use temporary array and then delete the real one if you want to change it while in cycle:

$array = array(0 => 'a', 1 => 'b', 2 => 'c');
$temp_array = $array[1];

unset($array[1]);
array_unshift($array , $temp_array);

the output will be:

array(0 => 'b', 1 => 'a', 2 => 'c')

and when are doing it while in cycle, you should clean $temp_array after appending item to array.

Upvotes: 4

Trav L
Trav L

Reputation: 15192

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
)

Upvotes: 494

tihe
tihe

Reputation: 2472

In case of an associative array or numbered array where you do not want to change the array keys:

$firstItem = array('foo' => 'bar');

$arr = $firstItem + $arr;

array_merge does not work as it always reindexes the array.

Upvotes: 154

Related Questions