Home > Backend Development > PHP Tutorial > Detailed explanation of the usage of PHP's array_unshift() function

Detailed explanation of the usage of PHP's array_unshift() function

PHPz
Release: 2023-06-27 12:12:01
Original
2689 people have browsed it

array_unshift() is the array function that comes with PHP. It inserts one or more elements into the beginning of the array and then recalculates the index. This function can very conveniently insert elements into the head of the array and implement operations on data structures such as queues.

The syntax of the array_unshift() function is as follows:

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

Among them, $array is the array into which elements are to be inserted; $value1, $value2 are one or more values ​​to be inserted.

Sample code:

$cars = array("Volvo", "BMW", "Toyota");
array_unshift($cars, "Mercedes");
print_r($cars);
Copy after login

Output result:

Array
(
    [0] => Mercedes
    [1] => Volvo
    [2] => BMW
    [3] => Toyota
)
Copy after login

In this example, we insert an element "Mercedes" at the beginning of the $cars array and use print_r( ) function outputs the added array.

If you want to insert multiple elements into the array at one time, you can use array_unshift() as follows:

$cars = array("Volvo", "BMW", "Toyota");
array_unshift($cars, "Mercedes", "Jeep", "Honda");
print_r($cars);
Copy after login

Output result:

Array
(
    [0] => Mercedes
    [1] => Jeep
    [2] => Honda
    [3] => Volvo
    [4] => BMW
    [5] => Toyota
)
Copy after login

In this example, we will Elements are inserted into the beginning of the $cars array at the same time, realizing the function of quickly adding elements to the queue.

In addition to basic usage, the array_unshift() function can also be applied to other application scenarios.

For example, you can use the array_unshift() function to delete the last element of an array. The sample code is as follows:

$cars = array("Volvo", "BMW", "Toyota");
array_unshift($cars, "Mercedes");
array_pop($cars);
print_r($cars);
Copy after login

Output result:

Array
(
    [0] => Mercedes
    [1] => Volvo
    [2] => BMW
)
Copy after login

In this example, we first use the array_unshift() function to insert an element to the beginning of the $cars array, and then use the array_pop() function The last element of the array, "Toyota", is removed. Finally, use the print_r() function again to output the array after the operation.

In summary, the array_unshift() function is a very practical array function in PHP. It can easily insert elements into the array, and allow the inserted array index to be recalculated, realizing a lot of convenient data operate.

The above is the detailed content of Detailed explanation of the usage of PHP's array_unshift() function. 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