How to remove first element from an array in PHP?
To remove the first element or value from an array, array_shift() function is used. This function also returns the removed element of the array and returns NULL if the array is empty. After removing first element, the key of the other elements is modified, and again array is numbered from beginning, only if the keys are numerical.
It is an inbuilt array function of PHP, which shifts an element from the beginning of the array.
The array_shift() function, which is used to remove the first element from an array, returns the removed element. It also returns NULL, if the array is empty.
For example: Using string elements
An element "Blue" is removed from the first position in the given array, and updated list is displayed in the given output.
Arraylist: Array (  => Blue  => Red  => Black  => Green  => Gray  => White ) Removed element from array is: Blue Updated arraylist: Array (  => Red  => Black  => Green  => Gray  => White )
Example: Using numeric keys
Removed element: Carom Array (  => Chess  => Ludo )
Example: Using numeric values
Removed array element is: 25 Update array is: Array (  => 12  => 65 2] => 37  => 95  => 38  => 12 )
Next TopicPHP Tutorial