How to useunset() Function in PHP
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
$arr = array(10, 20, 30, 40, 50, 60);
unset($fruits[0]);
echo "Updated array: ";
print_r($arr);
?>
Output
Updated array: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 [5] => 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