What is the usage of php array pop

PHPz
Release: 2023-04-26 14:16:26
Original
753 people have browsed it

php array pop usage

In PHP development, array is a very common data structure, which can store elements of multiple data types. In array operations, there is a commonly used function called array_pop(). This function can delete an element from the end of the array and return the element. This article will introduce the usage and examples of the php array pop function in detail.

  1. The syntax and parameters of the array_pop function

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

mixed array_pop (array &$array)

Parameter description:

$array: required. The array to delete elements from, or NULL if the array is empty.

Return value: Returns the value of the deleted element. If the array is empty, the function returns NULL.

  1. Example of array_pop function

The following will introduce the specific usage of php array_pop function with examples.

2.1 Delete the last element of the array

Through the array_pop function, you can easily delete the last element of the array. The specific example is as follows:

$fruits = ['apple', 'banana', 'orange'];
$last_fruit = array_pop($fruits);
print_r($fruits);  //输出为:['apple', 'banana']
echo $last_fruit;  //输出为:orange
Copy after login

In the example, We first define an array containing three types of fruits. Next, through the array_pop function, we delete the last element of the array and assign it to the $last_fruit variable. Finally, we print out the $fruits array and find that the last element has been deleted. And through echo output $last_fruit you can see that the deleted element is 'orange'.

2.2 Delete all array elements

In addition to the above method of deleting the last element, the array_pop function can also delete all elements in the array through a loop. The sample code is as follows:

$fruits = ['apple', 'banana', 'orange'];
while ($fruit = array_pop($fruits)) {
    echo $fruit . "\n";
}
Copy after login

In this example, we first define an array containing three kinds of fruits, and then use a while loop to continuously perform the array_pop operation to delete elements from the array and output it to the terminal. The loop ends when the array is empty.

2.3 Used in combination with array_push

In the actual development process, we usually need to delete the last element in the array and add it to another array. At this time, you can use the array_push function to complete it. The sample code is as follows:

$fruits = ['apple', 'banana', 'orange'];
$removed_fruit = array_pop($fruits);
$basket = [];
array_push($basket, $removed_fruit);
print_r($fruits);   //输出为:["apple", "banana"]
print_r($basket);   //输出为:["orange"]
Copy after login

In the example, we first define an array $fruits containing three kinds of fruits. Next, we delete the last element 'orange' from the array $fruits through the array_pop function and assign it to the $removed_fruit variable. Finally, we define an empty array named $basket and add $removed_fruit to the $basket array via the array_push function. Finally, the printout shows that the array operation is correct.

  1. Summary

The array_pop function is a very commonly used array operation function in PHP. It can easily delete the last element of the array and return the deleted element. We can easily manipulate arrays with the help of this function. We can use the array_pop function with a while loop to easily delete all array elements. When used in conjunction with array_push, elements deleted from the array can be added to other arrays. Proficiency in the use of array_pop function plays an important role in PHP array operations.

The above is the detailed content of What is the usage of php array pop. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!