Array Functions

PHP array_pop() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_pop() function removes the last element of an array.

The following table summarizes the technical details of this function.

Return Value: Returns the value of the last element of array.
If array is empty or is not an array, NULL will be returned.
Version: PHP 4+

Syntax

The basic syntax of the array_pop() function is given with:

array_pop(array);

The following example shows the array_pop() function in action.

<?php
// Sample array
$cities = array("London", "Paris", "New York", "Sydney");
    
// Remove and get the last value from array
echo array_pop($cities); // Prints: Sydney
print_r($cities);
?>

Parameters

The array_pop() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to get the last value from.

More Examples

Here're some more examples showing how array_pop() function actually works:

This function can also be used with the associative array, as shown here:

<?php
// Sample array
$alphabets = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "d"=>"dog");
    
// Remove and get the last value from array
echo array_pop($alphabets); // Prints: dog
print_r($alphabets);
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties