PHP array_pop() function
Syntax
PHP array_pop() function has the following syntax.
mixed array_pop ( array &arr )
Definition
The array_pop()
function returns the value
from the end of the array and removes it from the array.
array_pop() is the counterpart to array_shift();
Parameter
- array - Required. Array to popup
Note
array_push()
and array_pop()
are
useful for creating a stack which is a last-in, first-out
(LIFO) structure.
We can add new values onto the "top" of the
stack with array_push()
, then retrieve the most recently
added value with array_pop()
.
Example
Popup value from an Array
<?PHP/*w w w. j a va 2 s . c om*/
$names = array("Java", "PHP", "Python", "HTML","java2s.com", "CSS");
$firstname = array_pop($names);
print($firstname);
print_r($names);
?>
The code above generates the following result.
Example 2
Popup element from associate array
<?PHP/*from www . j av a2s. c om*/
$myBook = array( "title" => "Learn PHP from java2s.com",
"author" => "java2s.com",
"pubYear" => 2000 );
echo array_pop( $myBook ) . "\n";
print_r( $myBook );
?>
The code above generates the following result.