Home  >  Article  >  Backend Development  >  PHP function introduction—array_unshift(): insert elements into the beginning of the array

PHP function introduction—array_unshift(): insert elements into the beginning of the array

PHPz
PHPzOriginal
2023-07-25 21:41:051960browse

PHP function introduction—array_unshift(): Insert elements into the beginning of the array

In PHP, arrays are one of the most commonly used data structures. When we need to insert a new element to the beginning of the array, we can use PHP's array_unshift() function to achieve this.

The function of array_unshift() function is to insert one or more elements at the beginning of the array and change the length of the array. It inserts new elements into the head of the array and moves the existing elements backward.

The syntax of array_unshift() function is as follows:
array_unshift(array &$array, mixed $value1 [, mixed $... ])

Parameter description:

  • &$array: required, the array to be operated on, passed by reference.
  • $value1: required, the element to be inserted, which can be one or more.

Let’s look at a specific sample code:

<?php
$fruit = array("apple", "banana", "orange");

echo "原数组:";
print_r($fruit);

array_unshift($fruit, "lemon");
echo "插入后的新数组:";
print_r($fruit);
?>

In the code, we created an array named $fruit, which contains 3 fruits. We then use the array_unshift() function to insert a new element "lemon" to the beginning of the array. Then print out the inserted array through the print_r() function.

Run the above code, we will get the following output:

Original array: Array
(

[0] => apple
[1] => banana
[2] => orange

)
New array after insertion: Array
(

[0] => lemon
[1] => apple
[2] => banana
[3] => orange

)

As can be seen from the output results, through the array_unshift() function, we successfully inserted the new element "lemon" into the beginning of the array, and the original elements Moved one position backward in turn.

It should be noted that the array_unshift() function returns the new array length after inserting elements.

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(4, 5);

echo "原数组:";
print_r($numbers);

array_unshift($numbers, 1, 2, 3);
echo "插入后的新数组:";
print_r($numbers);
?>

Run the above code, we will get the following output:

Original array: Array
(

[0] => 4
[1] => 5

)
New array after insertion :Array
(

[0] => 1
[1] => 2 
[2] => 3
[3] => 4
[4] => 5

)

Through the above example, we learned how to use the array_unshift() function to insert one or more elements to the beginning of the array and change the length of the array.

Summary:
The array_unshift() function is one of the commonly used array functions in PHP. It can easily insert one or more elements at the beginning of the array. In actual development, when we need to add elements to the beginning of the array, we can use the array_unshift() function to achieve this.

The above is the detailed content of PHP function introduction—array_unshift(): insert elements into 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