Function array_merge exists since PHP 4:
<?php
$data = [1, 3, 4];
$values = [5, 6];
$data = array_merge($values, $data);
print_r($data);
Live PHP sandbox
Answer from Slava Rozhnev on Stack OverflowW3Schools
w3schools.com › php › func_array_unshift.asp
PHP array_unshift() Function
The array_unshift() function inserts new elements to an array.
PHP
php.net › manual › en › function.array-unshift.php
PHP: array_unshift - Manual
If one associative array is prepended to another associative array, the prepended array is numerically indexed into the former array. <?php $foods = [ 'apples' => [ 'McIntosh' => 'red', 'Granny Smith' => 'green', ], 'oranges' => [ 'Navel' => 'orange', 'Valencia' => 'orange', ], ]; $vegetables = [ 'lettuce' => [ 'Iceberg' => 'green', 'Butterhead' => 'green', ], 'carrots' => [ 'Deep Purple Hybrid' => 'purple', 'Imperator' => 'orange', ], 'cucumber' => [ 'Kirby' => 'green', 'Gherkin' => 'green', ], ]; array_unshift($foods, $vegetables); var_dump($foods); ?>
Videos
00:57
Array_shift dan Array_unshift #skysen #coding #pemograman #php ...
01:51
PHP Array Shift and UnShift | PHP Tutorial - YouTube
01:03
The array_shift and array_unshift Functions in PHP - YouTube
03:44
#31 - array_unshift in PHP || PHP Array Functions - YouTube
01:21
Array : PHP: Array_unshift an not numeric index - YouTube
PHP Array Push Pop Shift UnShift Functions Explained in ...
What is the difference between array_unshift and array_push ?
$items = array("pen", "pencil");
array_unshift($items, "eraser"); // Add at start
array_push($items, "marker"); // Add at end
print_r($items);
array_unshift adds values at the start, array_push adds at the end.flatcoding.com
flatcoding.com › home › php array_unshift: how to add values to the start of an array
PHP array_unshift: How to Add Values to the Start of an Array - ...
Can array_unshift add multiple elements at once?
$nums = array(3, 4);
array_unshift($nums, 1, 2);
print_r($nums);
Yes, array_unshift accepts multiple values separated by commas.flatcoding.com
flatcoding.com › home › php array_unshift: how to add values to the start of an array
PHP array_unshift: How to Add Values to the Start of an Array - ...
GeeksforGeeks
geeksforgeeks.org › php › php-array_unshift-function
PHP array_unshift() Function - GeeksforGeeks
June 20, 2023 - In the above program, we have seen that if a non-key array is passed to the array_unshift() function then it is automatically modified to an array with numeric keys. But if the array already had numeric keys starting from zero then after inserting new elements the keys will get modified. ... <?php // PHP program to illustrate // the use of array_unshift() // Input Array $array = array(1=>"ram", 2=>"krishna", 3=>"aakash"); // Values to be inserted $a1 = "rohan"; $a2 = "rajeeb"; $a3 = "saniya"; // Calling array_unshift() array_unshift($array, $a1, $a2, $a3); // Print modified array print_r($array); ?>
PHP Tutorial
phptutorial.net › home › php tutorial › php array_unshift
PHP array_unshift() Function
April 6, 2025 - Since the array_unshift() function adds the new elements to the beginning of the input array, it changes the indexes to start from zero. Let’s take some examples of using the PHP array_unshift() function.
Jobtensor
jobtensor.com › Tutorial › PHP › en › Array-Functions-array_unshift
PHP Built-in array_unshift(), Definition, Syntax, Parameters, Examples | jobtensor
The array_unshift() function inserts one or more elements at the beginning of an array. Numeric keys will start at 0 and increase by 1. String keys will remain the same. ... <?php // Example 1 $cities = array("New York", "Salt Lake", "Tokyo"); array_unshift($cities, "Berlin", "London"); ...
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › php › func_array_unshift.asp.html
PHP array_unshift() Function
PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones ... The array_unshift() function inserts new elements to an array.
Top answer 1 of 2
2
Function array_merge exists since PHP 4:
<?php
$data = [1, 3, 4];
$values = [5, 6];
$data = array_merge($values, $data);
print_r($data);
Live PHP sandbox
2 of 2
2
You should use array_merge instead of array_unshift.
$data = [1, 3, 4];
$values = [5, 6];
$result = array_merge($values, $data); // the sequence of the array inside array_merge will decide which array should be merged at the beginning.
OnlinePHP
onlinephp.io › array-unshift › manual
array_unshift - OnlinePHP.io Example
array_unshift prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order.
Top answer 1 of 5
10
The API of ArrayObject does not have any function to accomplish this directly. You have several other options:
- Manually move each element by 1, and set your new value at index 0 (only if you have a numerically index ArrayObject).
$tmp = NULL;
for ($i = 0; $arrayObject->offsetExists($i + 1); $i++) {
$tmp = $arrayObject->offsetGet($i + 1);
$arrayObject->offsetSet($i + 1, $arrayObject->offsetGet($i));
}
$arrayObject->offsetSet($i, $tmp);
$arrayObject->offsetSet(0, $new_value);
- Write a class deriving from
ArrayObjectand add a functionprepend(implementation could be the one below). - Extract an array, call
array_unshift()and create a newArrayObjectwith the modified array:
$array = $arrayObject->getArrayCopy();
array_unshift($array, $new_value);
$arrayObject->exchangeArray($array);
2 of 5
6
There is no such functionality in ArrayObject, but you can subclass it to add whatever you need. Try this:
class ExtendedArrayObject extends ArrayObject {
public function prepend($value) {
$array = (array)$this;
array_unshift($array, $value);
$this->exchangeArray($array);
}
}
ZetCode
zetcode.com › php-array › array-unshift
PHP array_unshift - Add Elements to Array in PHP
The PHP array_unshift function prepends one or more elements to the beginning of an array. It modifies the original array and returns the new count of elements. array_unshift adds elements to the start of an array. All numeric array keys are re-indexed starting from zero.
Top answer 1 of 9
500
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
)
2 of 9
155
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.
Educative
educative.io › answers › what-is-the-arrayunshift-method-in-php
What is the array_unshift method in PHP?
Non-numeric keys remain unchanged. This method returns the new length of the array after it appends the passed elements. ... We created an array called numbers. We created a printArray function to print the array.
Top answer 1 of 4
6
You could use
array_merge()
For example
$resultingArray = array_merge(array($newElement), $originalArray);
2 of 4
3
Next to array_merge, if there ain't any duplicate keys, you can do:
$array = array('a' => 'A');
$append = array('b' => 'hello');
$array = $append + $array;
Gives:
Array
(
[b] => hello
[a] => A
)
The plus is the array union operatorDocs.
GitHub
github.com › php › doc-en › blob › master › reference › array › functions › array-unshift.xml
doc-en/reference/array/functions/array-unshift.xml at master · php/doc-en
</informaltable> </para> </refsect1> · <refsect1 role="examples"> &reftitle.examples; <para> <example> <title><function>array_unshift</function> example</title> <programlisting role="php"> <![CDATA[ <?php · · $queue = [ "orange", "banana" ]; ·
Author php