To remove the first item from an array in PHP, you can use the array_shift()
function. This function removes the first element from an array, and returns the value of the removed element.
For example:
$fruits = array('apple', 'banana', 'cherry');
$first = array_shift($fruits);
// $first will be equal to 'apple'
// $fruits will be equal to array('banana', 'cherry')
You can also use the unset()
function to remove an element from an array by its key. For example:
$fruits = array('apple', 'banana', 'cherry');
unset($fruits[0]);
// $fruits will be equal to array(1 => 'banana', 2 => 'cherry')
Note that using unset()
will renumber the keys of the array, starting from 0. If you want to preserve the keys, you can use the array_slice()
function to remove the first element of the array and return a new array with the remaining elements.
For example:
$fruits = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry');
$fruits = array_slice($fruits, 1);
// $fruits will be equal to array('b' => 'banana', 'c' => 'cherry')