array_shift()
The 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.
Syntax
array_shift($array)
$array
is the input array.
Example
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 $counting_rhyme
array.
<?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([0] => Meeny[1] => Miny[2] => Moe)
Codebyte Example
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 0
.
All contributors
- Anonymous contributorAnonymous contributor1 total contribution
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.