How to Remove Last Element from an Array in PHP ?
Last Updated :
23 Jul, 2025
Removing the last element from an array is a common operation in PHP. There are several approaches to achieve this. In this article, we will explore different methods to remove the last element from an array.
Using array_pop() Function
The array_pop() function is specifically designed to remove the last element from an array and return its value.
PHP
<?php
$arr = [1, 2, 3, 4, 5];
// Remove last element using
// array_pop() function
$lastElement = array_pop($arr);
// Display the modified array and
// the removed element
echo "Modified Array: ";
print_r($arr);
echo "Removed Element: $lastElement";
?>
OutputModified Array: Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Removed Element: 5 Using array_slice() Function
Another approach is to use array_slice() function to create a new array without the last element.
PHP
<?php
$arr = [1, 2, 3, 4, 5];
// Remove last element
$arr = array_slice($arr, 0, -1);
// Display the modified array
echo "Modified Array: ";
print_r($arr);
?>
OutputModified Array: Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
) Using unset() Function
The unset() function can also be used to remove the last element, but it works by unsetting the array element at a specific index.
PHP
<?php
$arr = [1, 2, 3, 4, 5];
// Remove the last element
unset($arr[count($arr) - 1]);
// Display the modified array
echo "Modified Array: ";
print_r($arr);
?>
OutputModified Array: Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
) Using array_splice
In this approach we use array_splice function that can be used to remove the last element.
PHP
<?php
$sourceArray = array(1, 2, 3, 4, 5);
array_splice($sourceArray, -1, 1);
print_r($sourceArray);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Using array_shift() after reversing the array
In PHP, you can remove the last element from an array by reversing the array with array_reverse(), using array_shift() to remove the first element (previously the last), and then reversing the array back to its original order.
Example
PHP
<?php
$array = [1, 2, 3, 4, 5];
// Reverse the array
$array = array_reverse($array);
// Remove the first element (which was originally the last)
array_shift($array);
// Reverse the array back to its original order
$array = array_reverse($array);
// Output the modified array
print_r($array); // Output: [1, 2, 3, 4]
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Using count() and unset()
This method involves using the count() function to determine the number of elements in the array and then using the unset() function to remove the last element by targeting its index directly. This is a straightforward approach when you do not wish to modify the array's keys and when performance is a consideration since it avoids creating a new array or altering the array's natural order.
Example: Here’s how you can use the count() and unset() functions to remove the last element from an array:
PHP
<?php
$fruits = ['apple', 'banana', 'cherry'];
// Remove the last element
unset($fruits[count($fruits) - 1]);
print_r($fruits);
?>
OutputArray
(
[0] => apple
[1] => banana
)
Using array_filter() Function
The array_filter() function can be creatively used to remove the last element by applying a callback that excludes the last element based on its key or index.
Example:
PHP
<?php
// Example array
$array = [1, 2, 3, 4, 5];
// Remove the last element using array_filter()
$newArray = array_filter($array, function($key) use ($array) {
return $key !== array_key_last($array);
}, ARRAY_FILTER_USE_KEY);
print_r($newArray);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance