array_shift() pops the first value of the array off and returns it, shortening the array by one element and shifting each element down. Numerical keys will be reset to start counting from
0, whereas the remaining literal keys will not be affected.
$arrayis the input array.
The following example shifts the first element in
$counting_rhyme and assigns the returned value to the
$removed variable, then it prints out
$removed and the modified
<?php$counting_rhyme = ["Eeny", "Meeny", "Miny", "Moe"];$removed = array_shift($counting_rhyme);echo $removed . "\n";print_r($counting_rhyme)?>
The above code results in the following output:
EenyArray( => Meeny => Miny => Moe)
In the code snippet below
array_shift() is used twice. The first time it pops the element with a numerical key, the second time an element with a literal key. After that the
$num_array is printed out. Notice that the string
"600" is considered a numerical key, so it is reset to
- Anonymous contributorAnonymous contributor1 total contribution
- Anonymous contributor