Home > Article > Backend Development > How to use the function array_unshift() in php to insert elements at the beginning of the array
Before we knew this function, it seemed very complicated to insert some elements in front of the data. We had to use traversal to operate, or insert them to the back. Below we share a practical array_unshift() function, which can solve all problems.
Let’s not say anything but let’s look at the example
#1 array_unshift() example
<!--?php $queue = array("orange", "banana");//定义数组 array_unshift($queue, "apple", "raspberry");//向数组插入元素 print_r($queue); ?>
Output result
Array ( [0] => apple [1] => raspberry [2] => orange [3] => banana )
array_unshift() function is in Insert one or more elements at the beginning of the array.
It's very simple, let's take a look at the array_unshift() description.
The added elements are added as a whole, and the order of these elements in the array is the same as the order in the parameters.
This function will return the number of elements in the array.
A very simple example of a function
array_unshift(array,value1,value2,value3...)