Home >Backend Development >PHP Tutorial >Use the PHP function 'array_unshift' to insert elements to the beginning of the array

Use the PHP function 'array_unshift' to insert elements to the beginning of the array

WBOY
WBOYOriginal
2023-07-25 15:39:15821browse

Use the PHP function "array_unshift" to insert an element at the beginning of the array

PHP is a widely used server-side scripting language for creating dynamic web pages. In PHP, an array is a very important data structure used to store and manipulate a set of data. Sometimes we need to insert an element at the beginning of the array, then we can use the PHP built-in function "array_unshift".

The "array_unshift" function is to insert one or more elements into the beginning of the array and return the number of elements in the array after insertion. Its syntax is as follows:

array_unshift(array $array, mixed $value1 [, mixed $[value2 …]])

Among them, $array is the array to insert elements, $value1, $value2 is the element to be inserted.

The following is a simple example that demonstrates how to use the "array_unshift" function to insert elements to the beginning of the array:

<?php
$fruits = array("apple", "orange", "banana");
echo "Before array_unshift: ";
print_r($fruits);

array_unshift($fruits, "grape");
echo "After array_unshift: ";
print_r($fruits);
?>

Run the above code, the output is as follows:

Before array_unshift: Array
(
    [0] => apple
    [1] => orange
    [2] => banana
)
After array_unshift: Array
(
    [0] => grape
    [1] => apple
    [2] => orange
    [3] => banana
)

In this example, the initial array contains three fruits: apples, oranges and bananas. After inserting "grape" at the beginning of the array using the "array_unshift" function, the array becomes an array of four elements, with "grape" in the first position.

In addition to inserting one element, we can also use the "array_unshift" function to insert multiple elements at once. For example:

<?php
$numbers = array(3, 4, 5);
echo "Before array_unshift: ";
print_r($numbers);

array_unshift($numbers, 1, 2);
echo "After array_unshift: ";
print_r($numbers);
?>

Run the above code, the output is as follows:

Before array_unshift: Array
(
    [0] => 3
    [1] => 4
    [2] => 5
)
After array_unshift: Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

In this example, the initial array contains three numbers: 3, 4 and 5. After using the "array_unshift" function to insert 1 and 2 at the beginning of the array, the array becomes an array of five elements, with 1 and 2 in the first two positions.

To summarize, the PHP function "array_unshift" is a very convenient way to insert elements to the beginning of an array. It can be used to insert one or more elements without manually re-indexing the array. In actual projects, we can flexibly use this function to meet different needs.

The above is the detailed content of Use the PHP function 'array_unshift' to insert elements to the beginning of the array. For more information, please follow other related articles on the PHP Chinese website!

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