Home  >  Article  >  Backend Development  >  How to use the function array_unshift() in php to insert elements at the beginning of the array

How to use the function array_unshift() in php to insert elements at the beginning of the array

PHP中文网
PHP中文网Original
2017-04-24 10:47:512455browse

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...)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn