Using PHP's array_pop Function to Remove the Last Element from an Array
This article explains how PHP's array_pop function removes and returns the last element of an array, demonstrates its syntax, provides a complete code example with output, and highlights its usefulness for backend array manipulation.
In PHP, handling array data often requires extracting the last element for further processing, and the built‑in array_pop function provides a convenient way to do this.
The function removes the final element from the given array and returns it, or returns null if the array is empty. mixed array_pop ( array &$array ) Below is a practical example that defines an array of fruit names, uses array_pop to pop the last fruit, and then prints both the popped value and the remaining array.
<?php<br/>// Define an array<br/>$fruits = array("apple", "banana", "orange", "grape");<br/><br/>// Pop and return the last element<br/>$last_fruit = array_pop($fruits);<br/><br/>// Output the popped element<br/>echo "弹出的元素是:" . $last_fruit . "<br/>";<br/><br/>// Output the remaining array<br/>echo "剩余的数组是:<br/>";<br/>print_r($fruits);<br/>?>Running the script produces the following output, showing that "grape" was removed and the remaining array contains the other three fruits.
弹出的元素是:grape<br/>剩余的数组是:<br/>Array<br/>(<br/> [0] => apple<br/> [1] => banana<br/> [2] => orange<br/>)Thus, array_pop is a simple yet powerful tool for backend developers who need to manipulate array data efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
