Home > Backend Development > PHP Tutorial > How to add an element at the beginning of an array using array_unshift function in PHP

How to add an element at the beginning of an array using array_unshift function in PHP

王林
Release: 2023-06-26 13:56:02
Original
925 people have browsed it

PHP is a widely used server-side scripting language. Its application scenarios involve many aspects such as website development and data processing. The array is one of the basic data types of PHP and one of the most commonly used data structures.

In PHP array operations, it is often necessary to add an element to the beginning of an array. PHP's array_unshift function is used to implement this function. The array_unshift function inserts one or more elements at the beginning of the array and returns the length of the new array. Let's take a look at how to use this function.

First, we need to have an array. Here is a simple array example:

$fruits = array('apple', 'banana', 'orange');
Copy after login

Now we want to add an element at the beginning of this array: 'pear'.

For this, we can use the array_unshift function. The syntax of the function is as follows:

array_unshift($array, $value1, $value2, ...);
Copy after login

Among them, $array represents the array to be operated on, $value1, $value2, ... represents the value to be inserted at the beginning of the array.

We modify the above code and add the array_unshift function, as follows:

$fruits = array('apple', 'banana', 'orange');
array_unshift($fruits, 'pear');
print_r($fruits);
Copy after login

Run the above code, the output result is as follows:

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

You can see, 'pear' has been added to the beginning of the array.

It should be noted that the array_unshift function can insert multiple elements at the same time. For example:

$fruits = array('apple', 'banana', 'orange');
array_unshift($fruits, 'pear', 'grape', 'kiwi');
print_r($fruits);
Copy after login

Run the above code, the output result is as follows:

Array
(
    [0] => pear
    [1] => grape
    [2] => kiwi
    [3] => apple
    [4] => banana
    [5] => orange
)
Copy after login

You can see that the three elements 'pear', 'grape', and 'kiwi' have been added to the beginning of the array.

It should be noted that the array_unshift function will change the index value of the array, so you need to pay special attention when using it.

In summary, it is very simple to add an element to the beginning of an array using the array_unshift function in PHP. Just use the array_unshift function and specify the value to be inserted in the function's parameters. Just pay attention to the index value of the array when using it.

The above is the detailed content of How to add an element at the beginning of an array using array_unshift function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template