PHP Function Reference

PHP array_pop() Function



The PHP array_pop() function is used to delete the last element of the given array. The function returns the value of the last element (deleted element) of the array.

Syntax

array_pop(array)

Parameters

array Required. Specify an array.

Return Value

Returns the value of the last element of array. If array is empty (or is not an array), null will be returned.

Exceptions

Throws E_WARNING, if the function is called on a non-array.

Example:

The example below shows the usage of array_pop() function.

<?php
$Arr = array(10, 20, 30, 40);

//deleting the last element of the array
array_pop($Arr);

print_r($Arr);
?>

The output of the above code will be:

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
)

Example:

Consider the example below where array_pop() function is used with an associative array.

<?php
$Arr = array("Marry"=>25, 
             "John"=>30, 
             "Jo"=>45, 
             "Kim"=>22);

//deleting the last key/value pair
array_pop($Arr);

print_r($Arr);
?>

The output of the above code will be:

Array
(
    [Marry] => 25
    [John] => 30
    [Jo] => 45
)

❮ PHP Array Reference