How to Remove Last Element from an Array in PHP ?
Last Updated :
11 Jul, 2024
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
)
Similar Reads
JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
3 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read
How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i
4 min read
How to Remove First and Last Element from Array using JavaScript? Removing the first and last elements from an array is a common operation. This can be useful in various scenarios, such as data processing, filtering, or manipulation. Example:Input: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]Output: [ 2, 3, 4, 5, 6, 7, 8, 9 ]Removing first and last element in array can be do
2 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read