English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP array_unshift() Function Usage and Example
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 will remain the same. All numeric keys will be re-counted from zero, and all text keys will remain unchanged.
Returns a new array with the inserted elements.
Serial Number | Parameters 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 |
Use array_unshift to insert two elements at the beginning of an 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 )