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 Overflow
๐ŸŒ
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); ?>
People also ask

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.
๐ŸŒ
w3resource
w3resource.com โ€บ php โ€บ function-reference โ€บ array_unshift.php
PHP : array_unshift() function - w3resource
PHP: array_unshift() function: The array_unshift() is used to add one or more elements to the beginning of an array.
๐ŸŒ
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.
๐ŸŒ
FlatCoding
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 - FlatCoding
September 6, 2025 - PHP array_unshift helps you add new elements to appear before the existing ones. ... The array_unshift in PHP adds one or more values to the start of an array and moves the old keys.
Find elsewhere
๐ŸŒ
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"); ...
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ phps-array-shift-and-array-unshift-functions-a-complete-guide
PHP's `array_shift()` and `array_unshift()` Functions: A Complete Guide | Reintech media
April 14, 2023 - The `array_unshift()` function in PHP is a built-in function that adds one or more elements to the beginning of an array.
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
What is the best way to add something to the beginning of an array? - PHP - SitePoint Forums | Web Development & Design Community
June 7, 2015 - PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world. array_unshift() prepends passed elements to the front of the array.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ php-reference โ€บ php-array-unshift-function.php
PHP array_unshift() Function - Tutorial Republic
The following example shows the array_unshift() function in action. ... <?php // Sample array $colors = array("red", "green", "blue"); // Prepending two values to the colors array array_unshift($colors, "yellow", "orange"); print_r($colors); ?>
๐ŸŒ
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.
๐ŸŒ
3D Bay
clouddevs.com โ€บ home โ€บ php guides โ€บ unpacking the phpโ€™s array_unshift() function
Unpacking the PHP's array_unshift() Function
December 13, 2023 - Learn all about PHP's array_unshift() function โ€“ a powerful tool for adding elements to the beginning of an array. Dive into its syntax, use cases, and practical examples in this comprehensive guide.
๐ŸŒ
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.
๐ŸŒ
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.