Definition and Usage
- PHP Version
- 4+
The array_pop()
function removes the last element from an array and returns it.
Features
- Removes the last element from an array.
- Directly modifies the original array.
- This operation decreases the length of the array by one.
- Returns the removed element.
Basic Example
$array = ['orange', 'banana', 'apple'];
// Removes the last element from the array
array_pop($array);
print_r($array);
/*
Output:
Array
(
[0] => orange
[1] => banana
)
*/
The array_shift()
function is similar to array_pop()
, but it removes the first element from an array.
Syntax
array_pop(array &$array): mixed
Parameters
&$array |
The target array from which the last element will be removed (passed by reference). |
---|
Return Values
The array_pop()
function returns the last element removed from the array. If the array is empty and there is no element to remove, it returns null
.
array_pop()
// Initialize the array
$array = ['orange', 'banana', 'apple'];
// Remove the last element from the array
$removedItem = array_pop($array);
print_r($array);
/*
Output:
Array
(
[0] => orange
[1] => banana
)
*/
echo $removedItem; // Output: 'apple'
// Empty array
$empty_array = [];
$empty_array_removed_item = array_pop($empty_array); // Array is empty, no element to remove
var_dump($empty_array_removed_item); // Output: NULL
Important Notes and Use Cases
There are several key concepts and precautions to keep in mind when using the array_pop()
function.
- Return Value
- Modification of the Original Array and Array Length
- Works with Associative Arrays
- Reversing an Array Using a Loop
- Removing the Last Element with
unset()
Return Value
The array_pop()
function returns the removed element. You can store the returned value in a variable.
$fruits = ['apple', 'banana', 'cherry'];
$removedFruit = array_pop($fruits);
echo $removedFruit; // Output: 'cherry'
Modification of the Original Array and Array Length
The array_pop()
function modifies the original array. Therefore, when you remove an element, the array changes and its length decreases.
$fruits = ['apple', 'banana', 'cherry'];
array_pop($fruits);
echo count($fruits); // Output: 2
Works with Associative Arrays
The array_pop()
function works not only with indexed arrays but also with associative arrays. Since it removes and returns the last element, it is not restricted by array type.
$assocArray = ['name' => 'John', 'age' => 30];
array_pop($assocArray);
print_r($assocArray);
/*
Output:
Array
(
[name] => John
)
*/
Reversing an Array Using a Loop
You can reverse an array by combining array_pop()
with a loop.
// Original array
$fruits = ['apple', 'banana', 'cherry'];
// Create an empty array to hold reversed elements
$reversedFruits = [];
// Use a loop to pop elements from the original array and add them to the new array
while ($fruit = array_pop($fruits)) {
$reversedFruits[] = $fruit;
}
// Output the reversed array
print_r($reversedFruits);
/*
Output:
Array
(
[0] => cherry
[1] => banana
[2] => apple
)
*/
Note that PHP provides a built-in function array_reverse()
which reverses arrays more simply and intuitively:
$fruits = ['apple', 'banana', 'cherry'];
// Reverse the array
$reversedFruits = array_reverse($fruits);
// Output the reversed array
print_r($reversedFruits);
/*
Output:
Array
(
[0] => cherry
[1] => banana
[2] => apple
)
*/
Removing the Last Element with unset()
Instead of using array_pop()
, you can remove the last element of an array using the unset()
. The unset()
language construct destroys the specified variable.
// Original array
$fruits = ['apple', 'banana', 'cherry'];
// Remove the last element using unset()
unset($fruits[count($fruits) - 1]);
// Output the array
print_r($fruits);
/*
Output:
Array
(
[0] => apple
[1] => banana
)
*/
Using unset()
to remove specific elements from an array requires caution, and is therefore not recommended. If you remove an element from the middle of an array (instead of the first or last element), the index remains empty, and PHP does not reindex the array automatically.