Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

array_shift

Removes the first element of an array and returns it.

Syntax

array_shift(array &$array): mixed

Parameters

ParameterTypeDescription
$arrayarrayThe array being modified.

Return value

mixedThe removed element, or `null` for an empty array.

Examples

A queue: first in, first out:

<?php
 
$queue = ['Anna', 'Oleg', 'Ivan'];
$next = array_shift($queue);
 
echo $next, ' | ', implode(', ', $queue);

Output:

Anna | Oleg, Ivan

Numeric keys are renumbered:

<?php
 
$numbers = [10, 20, 30];
array_shift($numbers);
 
echo json_encode($numbers);

Output:

[20,30]

Notes

The function is slow on large arrays: every remaining element is shifted.

See also

  • array_pop — Removes the last element of an array and returns it.
  • array_unshift — Prepends elements to the beginning of an array.

In the tutorial

  • Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.

Thanks for reading!

Donate to the site — we work hard for you.