How to usearray_shift() Function in PHP
The array_shift() function removes the first element from an array and returns it. This function modifies the original array.
<?php
$arr = array(10, 20, 30, 40, 50, 60);
$removedElement = array_shift($arr);
echo "Removed element: " . $removedElement . "\n";
echo "Updated array: ";
print_r($arr);
?>
Output
Removed element: 10 Updated array: Array ( [0] => 20 [1] => 30 [2] => 40 [3] => 50 [4] => 60 )
How to Remove First Element from an Array in PHP?
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.
Table of Content
- Using array_shift() Function
- Using array_slice() Function
- Using unset() Function