PHP array_pop() function
In this chapter you will learn:
- Syntax for PHP array_pop() function
- Definition for PHP array_pop() function
- Parameter for PHP array_pop() function
- Note for PHP array_pop() function
- Example - Popup value from an Array
- Example - Popup element from associate array
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// ja va2 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// ja v a 2 s . c o m
$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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_product() Function
- Syntax for PHP array_product() Function
- Parameter for PHP array_product() Function
- Example - Calculate and return the product of an array
Home » PHP Tutorial » PHP Array Functions