Home > Backend Development > PHP Tutorial > 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中文网
Release: 2023-02-28 19:24:01
Original
2481 people have browsed it

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);
?>
Copy after login

Output result

Array
(
    [0] => apple
    [1] => raspberry
    [2] => orange
    [3] => banana
)
Copy after login

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...)
Copy after login
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template