English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online Tools

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

Object Function

PHP Array Function Manual

PHP array_unshift() Function Usage and Example

The array_unshift() function inserts one or more elements at the beginning of the array

Syntax1array_unshift($array,$value2array_unshift($array,$value3,$value

...)

The array_unshift() function inserts the unit into the beginning of the array. Note that the unit is inserted as a whole, so the order of the units inserted will remain the same. All numerical keys will be reset to start counting from zero, and all text keys will remain unchanged.

Return Value

 Returns a new array after inserting the elements.

Parameter

Serial NumberParameters and Description
1

array(Required)

It specifies an array.

2

value1(Required)

It specifies the value to be inserted

3

value2(Optional)

It specifies the value to be inserted

4

value3(Optional)

It specifies the value to be inserted

Online example

Use array_unshift to insert two elements at the beginning of the array and return the new array

<?php
   $input = array("orange", "banana");
   array_unshift($input, "apple", "pie");
   
   print_r($input);
?>
Test to see‹/›

Output result:

Array ( [0] => apple [1] => pie [2] => orange [3] => banana )

  PHP Array Function Manual