Remove First Element from an Array in PHP
Last Updated :
04 Jul, 2024
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, 2
Below are the methods to remove the first element from an array in PHP:
Using array_shift() function
The array_shift()
function removes the first element from an array and returns it. This function also re-indexes the array so that the keys start from 0 again.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
$arr = array(1, 2, 3, 4, 5, 6);
$removedElement = array_shift($arr);
echo "Removed element: " . $removedElement . "\n";
echo "Updated array: ";
print_r($arr);
?>
OutputRemoved element: 1
Updated array: Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
)
Using array_slice() function
The array_slice()
function is used to extract a portion of an array. By specifying a start index of 1, you can extract all elements except the first one.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
$arr = array(1, 2, 3, 4, 5, 6);
$arr = array_slice($arr, 1);
echo "Updated array: ";
print_r($arr);
?>
OutputUpdated array: Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
)
Using unset() Function
The unset() function is used to remove the first element from an array. This function takes the array and the index of the element to be removed as arguments.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
$arr = array(1, 2, 3, 4, 5, 6);
unset($arr[0]);
$arr = array_values($arr); // Re-index the array
echo "Updated array: ";
print_r($arr);
?>
OutputUpdated array: Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
)
Using array_splice() Function
The array_splice() function can remove elements from an array and can also replace them with other elements. By specifying the start index of 0 and a length of 1, you can remove the first element of the array.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
$arr = array(1, 2, 3, 4, 5, 6);
array_splice($arr, 0, 1);
echo "Updated array: ";
print_r($arr);
?>
OutputUpdated array: Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
)
Using array_shift() and array_values() functions
The array_shift() function removes the first element from an array and returns it. However, it also re-indexes the array starting from 0. To maintain the original indexing after removing the first element, you can use array_values().
Example:
PHP
<?php
// Sample array
$arr = [1, 2, 3, 4, 5, 6, 7];
// Remove the first element and re-index the array
array_shift($arr);
// Re-index the array to start from 0
$arr = array_values($arr);
// Output the modified array
print_r($arr);
?>
OutputArray
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
)
Similar Reads
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
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
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 Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', '
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
Remove All Occurrences of an Element in an Array Given an integer array arr[] and an integer ele the task is to the remove all occurrences of ele from arr[] in-place and return the number of elements which are not equal to ele. If there are k number of elements which are not equal to ele then the input array arr[] should be modified such that the
6 min read
How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
2 min read
How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function
5 min read