Javatpoint Logo
Javatpoint Logo

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.

Returns Value

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

Output

An element "Blue" is removed from the first position in the given array, and updated list is displayed in the given output.

Arraylist: Array ( [0] => Blue [1] => Red [2] => Black [3] => Green [4] => Gray [5] => White ) 
Removed element from array is: Blue
Updated arraylist: Array ( [0] => Red [1] => Black [2] => Green [3] => Gray [4] => White )

Example: Using numeric keys

Output

Removed element: Carom
Array ( [0] => Chess [1] => Ludo )

Example: Using numeric values

Output

An element 25 is removed from the first position in the given array, and updated list is displayed below.

Removed array element is: 25
Update array is: Array ( 
[0] => 12 
[1] => 65 
2] => 37 
[3] => 95 
[4] => 38 
[5] => 12 
)

Next TopicPHP Tutorial




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA