PHP array_shift() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_shift() removes the first item from an array and returns it.

After the shift, the remaining numeric keys are renumbered to start from 0. It is a handy way to treat an array like a queue.

Syntax

Call array_shift() with the values you need. Start with this line.

PHP
$first = array_shift($queue);
CallWhat it does
array_shift($queue)Removes and returns the first item.
$first = array_shift($list)Stores the removed item in $first.
array_shift($empty)Returns null when the array is empty.

array_shift() changes the original array because it is passed by reference. If you only want to look at the first item without removing it, use reset() or $array[0].

Library declarations
PHP
array_shift(array &$array): mixed

Parameters

ParameterDescription
arrayRequired. The array to take the first item from. It is passed by reference and is modified.

Return Value

Return ValueDescription
mixedThe value that was removed. Returns null if the array is empty.

PHP Examples

Example 1 — Remove the First Item

Take the first person off a waiting list.

PHP
<?php
$queue = ["Ann", "Ben", "Cara"];
$first = array_shift($queue);

echo "Removed: ", $first, PHP_EOL;
echo "Remaining: ", implode(", ", $queue), PHP_EOL;

How It Works

  1. Create a queue with three names.
  2. array_shift() removes "Ann" from the front and returns it.
  3. $queue now contains only "Ben" and "Cara".
  4. implode() joins the remaining names with commas for printing.

Example 2 — Reindexing and Empty Arrays

Numeric keys are renumbered, and an empty array gives null.

PHP
<?php
$nums = [5 => "a", 6 => "b", 7 => "c"];
array_shift($nums);
echo json_encode($nums), PHP_EOL;

$empty = [];
var_dump(array_shift($empty));

How It Works

  1. The array starts with keys 5, 6 and 7.
  2. After removing "a", PHP renumbers the remaining keys to 0 and 1.
  3. json_encode() prints a plain list because the keys are now 0 and 1.
  4. Shifting from an empty array returns null, shown by var_dump() as NULL.

Example 3 — Process a Queue in a Loop

Keep shifting until the queue is empty.

PHP
<?php
$queue = ["email", "resize", "backup"];

while (($task = array_shift($queue)) !== null) {
    echo "Processing ", $task, PHP_EOL;
}

echo "Left: ", count($queue), PHP_EOL;

How It Works

  1. The queue holds three task names.
  2. Each loop pass shifts the first task off the queue and stores it in $task.
  3. When the queue is empty, array_shift() returns null and the loop ends.
  4. count() confirms that nothing is left.

Frequently Asked Questions

It removes the first element of an array and returns it. The remaining items move down and numeric keys are renumbered from 0.
array_shift() removes from the start of the array, while array_pop() removes from the end.
Yes. Only numeric keys are renumbered. String keys stay the same.
It returns null and leaves the array unchanged.
Use array_unshift(), which is the opposite of array_shift().

Next: array_slice()

array_slice() returns a part of an array without changing the original.

array_slice() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful