Remove an Elements From End of an Array in PHP
Last Updated :
31 Jul, 2024
Given an array "myArray", the task is to remove an element from its end. This operation is important in scenarios involving stacks, queues, and web development. In PHP, there are several approaches to achieve this One common method is using the array_pop() function, which removes the last element from the array. For instance, if "myArray" initially contains string elements and we apply array_pop() to remove 'date' from its end, the modified array can be displayed using "print_r($myArray)", showcasing the updated contents without 'date'.
Example:
Input: $myArray = ["apple", "banana", "cherry", "date"];
Output: ["apple", "banana", "cherry"];
These are the following approaches:
Using the unset() function with the last index
We are going to use the unset() function in the PHP arrays which are the ordered maps and use the integer keys starting from 0. To remove an element from the end of an array using unset(), we specify the index of the last element in the array.
Example: To demonstrate removing an element from the end of the array using unset() function in PHP.
PHP
<?php
$array = [1, 2, 3, 4, 5];
echo "Original array: " . implode(", ", $array) . "\n";
$lastIndex = count($array) - 1;
unset($array[$lastIndex]);
echo "Modified array: " . implode(", ", $array) . "\n";
?>
OutputOriginal array: 1, 2, 3, 4, 5
Modified array: 1, 2, 3, 4
Using the array_pop() function
We are going to use the array_pop() function that is used to remove the last element from an array and returns the removed element.
Example: To demonstrate removing an element from the end of the array using arrya_pop() function in PHP.
PHP
<?php
$array = [1, 2, 3, 4, 5];
echo "Original array: " . implode(", ", $array) . "\n";
$removedElement = array_pop($array);
echo "Modified array: " . implode(", ", $array) . "\n";
echo "Removed element: " . $removedElement . "\n";
?>
OutputOriginal array: 1, 2, 3, 4, 5
Modified array: 1, 2, 3, 4
Removed element: 5
Using the array_slice() function
We are going to use the array_slice() function which returns a sequence of elements from the array, defined by the offset and length parameters. By slicing the array from the beginning to the second-to-last element, we effectively remove the last element.
Example: Removing an element from the end of the array using array_slice() function in PHP.
PHP
<?php
$array = [1, 2, 3, 4, 5];
echo "Original array: " . implode(", ", $array) . "\n";
$modifiedArray = array_slice($array, 0, -1);
echo "Modified array: " . implode(", ", $modifiedArray) . "\n";
?>
OutputOriginal array: 1, 2, 3, 4, 5
Modified array: 1, 2, 3, 4
Similar Reads
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
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
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 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
PHP | Remove duplicate elements from Array You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array. Examples: Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3} Output : array ( [6] => 2 [7] => 3 [4] => 1 [5] => 6 ) Input : array[] = {4, 2, 7, 3, 2, 7, 3} Output : arr
3 min read
Removing Array Element and Re-Indexing in PHP In order to remove an element from an array, we can use unset() function which removes the element from an array, and then use array_values() function which indexes the array numerically automatically. Function Usedunset(): This function unsets a given variable. Syntax:void unset ( mixed $var [, mix
2 min read