How to Remove First Element from an Array in PHP?
Last Updated :
17 Jul, 2024
Given an Array, the task is to remove the first element from the array in PHP. There are three methods to remove the first element from an array, these are - using array_shift(), array_slice(), and unset() functions. Let's explore each method with detailed explanations and code examples.
Approach 1: Using array_shift() Function
The array_shift() function removes the first element from an array and returns it. This function modifies the original array.
PHP
<?php
$arr = array(10, 20, 30, 40, 50, 60);
$removedElement = array_shift($arr);
echo "Removed element: " . $removedElement . "\n";
echo "Updated array: ";
print_r($arr);
?>
OutputRemoved element: 10
Updated array: Array
(
[0] => 20
[1] => 30
[2] => 40
[3] => 50
[4] => 60
)
Approach 2: Using array_slice() Function
The array_slice() function is used to extract a slice of an array. By specifying the start index as 1, you can remove the first element from the array.
PHP
<?php
$arr = array(10, 20, 30, 40, 50, 60);
$updated_array = array_slice($arr, 1);
echo "Updated array: ";
print_r($updated_array);
?>
OutputUpdated array: Array
(
[0] => 20
[1] => 30
[2] => 40
[3] => 50
[4] => 60
)
Approach 3: Using unset() Function
You can also use the unset() function to remove the first element from an array. This function takes the array and the index of the element to be removed as arguments.
PHP
<?php
$arr = array(10, 20, 30, 40, 50, 60);
unset($fruits[0]);
echo "Updated array: ";
print_r($arr);
?>
OutputUpdated array: Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
[4] => 50
[5] => 60
)
Approach 4: Using array_splice() Function
The array_splice() function can be used to remove elements from an array and optionally replace them with other elements.
PHP
<?php
$arr = [1, 2, 3, 4, 5];
array_splice($arr, 0, 1);
print_r($arr);
?>
OutputArray
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
Approach 5: Using the array_diff_key() Function
This method involves using the array_diff_key() function to remove the first element from an array by comparing keys. The array_diff_key() function compares the keys of two arrays and returns the differences. This approach is beneficial when you want to remove the first element based on its key, especially in associative arrays where keys are not necessarily numeric.
Example: Here’s how you can use array_diff_key() to remove the first element from both numeric and associative arrays. First, identify the key of the first element, then create a temporary array with this key and use array_diff_key() to remove it.
PHP
<?php
$array = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry'];
// Getting the first key of the array
$firstKey = array_key_first($array);
// Removing the first element
$modifiedArray = array_diff_key($array, [$firstKey => ""]);
print_r($modifiedArray);
?>
OutputArray
(
[b] => banana
[c] => cherry
)
Approach 6: 5. Using a Loop
In this approach we will shifting the all elements of the array to the left using a loop and then remove the last element.
PHP
<?php
$array = ['g', 'e', 'e', 'k'];
for ($i = 0; $i < count($array) - 1; $i++) {
$array[$i] = $array[$i + 1];
}
unset($array[count($array) - 1]);
print_r($array);
?>
OutputArray
(
[0] => e
[1] => e
[2] => k
)
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
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
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