PHP 8 reference · Array Functions
PHP array_shift() Function
Remove the first element of an array and return it. Numeric keys are renumbered from 0; string keys stay.
PHP 8 Since 4.0
Syntax
array_shift(array &$array): mixed
Parameters
| Parameter | Description |
|---|---|
array |
The array (changed in place). |
Return value
The removed value, or null if empty.
Examples
<?php
$queue = ["first", "second", "third"];
echo array_shift($queue), "\n";
print_r($queue);
Output
first
Array
(
[0] => second
[1] => third
)<?php
$args = ["script.php", "--verbose", "file.txt"];
$script = array_shift($args);
echo "$script got ", count($args), " arguments\n";
Output
script.php got 2 arguments
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
list.pop()· Pythonarray.shift()· JavaScript
Related
array_unshift()· Add one or more elements to the start of an array and return its new length. Numeric keys are renumbered.array_pop()· Remove the last element of an array and return it. Returns null if the array is empty.array_slice()· Return a portion of an array without changing the original, given an offset and an optional length.- PHP Arrays · tutorial
- PHP Array Functions · tutorial