Understanding PHP's array_shift Function
The array_shift function in PHP is an essential tool for manipulating arrays. This function works by removing the first element from an array and returning its
The array_shift function in PHP removes the first element from an array and returns its value. Because it always works on the front of the array, it is the natural tool for consuming a list from the start — processing a queue, peeling off a header row, or stripping an unwanted leading item.
Two things make array_shift distinct from simply unsetting $array[0]:
- It modifies the original array in place (passed by reference) — you don't build a new array.
- It re-indexes numeric keys, so the remaining elements always start again at
0. String keys are left untouched.
This page covers the syntax, the return value, the all-important re-indexing behaviour, a common loop gotcha, and several runnable examples.
Syntax
array_shift(array &$array): mixed| Part | Meaning |
|---|---|
&$array | The array to shift from. Passed by reference, so it is modified directly. |
| return value | The value of the removed first element, or null if the array is empty. |
Because the array is passed by reference, you call array_shift($colors) on a variable — not on a literal or a function result.
Removing the first element
The most common use is dropping the first element. The function mutates the array directly:
The result will be:
Array
(
[0] => green
[1] => blue
)Capturing the removed value
array_shift returns the element it removed, so you can keep it while shrinking the array in a single statement:
The result will be:
red
Array
(
[0] => green
[1] => blue
)Keys are re-indexed
This is the behaviour that surprises people most. array_shift does not just remove $array[0] — it re-numbers every remaining integer key from 0. String keys keep their names.
<?php
$data = [5 => "a", 10 => "b", "x" => "c"];
array_shift($data); // removes "a"
print_r($data);
?>The result will be:
Array
(
[0] => b
[x] => c
)Note that 10 => "b" became 0 => "b", while "x" => "c" was left alone. If you need to preserve the original numeric keys, use array_slice instead.
Processing an array as a queue
A common pattern is to drain an array from the front, processing one element at a time. The standard idiom uses array_shift inside a while loop:
The result will be:
Running: build
Running: test
Running: deployThe loop stops because array_shift returns null once the array is empty.
Gotcha: don't loop on a truthy test
You will often see this shorter form:
while ($value = array_shift($queue)) { ... }It works for strings like "build", but it is a trap. The condition is true only while the value is truthy, so the loop stops early on the first 0, "", "0", false, or null:
<?php
$numbers = array(3, 0, 1);
while ($n = array_shift($numbers)) {
echo "$n\n";
}
print_r($numbers); // 1 was never processed
?>The result will be:
3
Array
(
[0] => 1
)The loop quit at 0, leaving 1 behind. Always compare against null explicitly (!== null) when the values might be falsy.
Removing several leading elements
Calling array_shift repeatedly peels elements off the front. Here we drop the first three:
The result will be:
Array
(
[0] => yellow
[1] => orange
)To remove a leading chunk in one call instead of looping, reach for array_splice($colors, 0, 3).
Related functions
array_shift is one of four functions for adding and removing elements at the ends of an array:
| Function | Acts on | What it does |
|---|---|---|
| array_shift | Front | Removes and returns the first element |
| array_unshift | Front | Adds one or more elements to the start |
| array_pop | End | Removes and returns the last element |
| array_push | End | Adds one or more elements to the end |
For slicing without mutating the original array, see array_slice; to remove or replace a range, see array_splice.
Conclusion
array_shift removes the first element of an array, returns its value, modifies the array in place, and re-indexes the remaining integer keys from 0. It is ideal for consuming an array as a queue and for stripping unwanted leading items — just remember the re-indexing behaviour and the falsy-value loop trap covered above.