array_pop
mixed array_pop(array array)
Returns and deletes the last element of an array.
Returns:
Last element of the given array; NULL on failure or if the array is empty
Description:
array_pop() returns the last value of array
and deletes that element from the array. This also works for multidimensional arrays, so if the last element happens to be an array, that array is returned and deleted from array
.
This function can be used with array_push() to treat arrays as stacks.
Warning:
This function directly alters the array passed to it.
Version:
PHP 4
See also:
array_push()
array_shift()
array_unshift()
Example:
Pop elements from various arrays
$a1 = array('one', 'two', 'three', 'four');
$a2 = array('jan' => '01', 'feb' => '02', 'mar' => '03');
$a3 = array('one', 'two', 'three' => array('a', 'b', 'c'));
echo "First array before popping:\n";
print_r($a1);
$popped = array_pop($a1);
echo "First array after popping the value '$popped':\n";
print_r($a1);
echo "Second array before popping:\n";
print_r($a2);
$popped = array_pop($a2);
echo "Second array after popping the value '$popped':\n";
print_r($a2);
echo "Third array before popping:\n";
print_r($a3);
$popped = array_pop($a3);
echo "Third array after popping the final array:\n";
print_r($a3);
echo "Array popped from the end of the third array:\n";
print_r($popped);
Output:
First array before popping:
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
First array after popping the value 'four':
Array
(
[0] => one
[1] => two
[2] => three
)
Second array before popping:
Array
(
[jan] => 01
[feb] => 02
[mar] => 03
)
Second array after popping the value '03':
Array
(
[jan] => 01
[feb] => 02
)
Third array before popping:
Array
(
[0] => one
[1] => two
[three] => Array
(
[0] => a
[1] => b
[2] => c
)
)
Third array after popping the final array:
Array
(
[0] => one
[1] => two
)
Array popped from the end of the third array:
Array
(
[0] => a
[1] => b
[2] => c
)
|