The array_shift function simply removes the first element of the array and returns its value.
If the array consists of numeric keys then after the removal of the first element by array_shift function, the rest of the keys get a new keys string from 0.
What is the syntax of the array_shift function in PHP?
array_shift(array)
Parameter | Description |
---|---|
array | Required. Specifies an array |
Examples of array_shift function
<?php
$arr=array("a"=>"1","b"=>"2","c"=>"3");
echo array_shift($arr);
print_r($arr);
?>
In the above example, we remove the first element from the array using the array_shift function. It returns the value of the removed element.
<?php
$arr=array(0=>"R",1=>"G",2=>"B");
echo array_shift($arr);
print_r ($arr);
?>
In the above example, we use the array_shift function with the array having numeric keys. After the removal of the first element, the rest of the values will get new keys starting from 0.