How to Remove Array Last Element in PHP?

03-Apr-2023

.

Admin

How to Remove Array Last Element in PHP?

Hi Dev,

In this article, to remove the last element or value from an array, array_pop() function is used. this function returns the last removed element of the array and returns null if the array is empty, or is not an array

There are example to check if Remove Array Last Elementin php. in this example, we will use to array_pop() function to check if how to Remove Array Last Element in PHP. so let's the following example with output.

Example 1:


index.php

<?php

$car = array("Mercedes", "Creta", "Audi", "Chevrolet", "Skoda");

echo "Arraylist: ";

print_r($car);

$remove = array_pop($car);

echo "</br>Removed element from array is: ";

print_r($remove);

echo "</br>Updated arraylist: ";

print_r($car);

?>

Output:

Arraylist: Array ( [0] => Mercedes [1] => Creta [2] => Audi [3] => Chevrolet [4] => Skoda )

Removed element from array is: Skoda

Updated arraylist: Array ( [0] => Mercedes [1] => Creta [2] => Audi [3] => Chevrolet )

I hope it could help you...

#PHP