In this tutorial we will learn how to remove last element of an array using PHP, we can use PHP array_pop() function to remove last value or element of an array.
Table of Contents
PHP array_pop() function
PHP array_pop() function removes or deletes the last element of array and returns the last value as a result.
We can also use array_pop() function to get last value of array.
array_pop() function returns NULL if array is empty.
<?php
$array = array("Red", "Black", "Blue", "Yellow");
print_r($array);
echo "<br />";
$lastElement = array_pop($array);
print_r($array); // array without last element
?>
The above code will remove the last element of array.