PHP array_shift() function


The array_shift() function in PHP is used to remove the first element from an array and return that element. This function modifies the original array by shifting all other elements to the left, effectively reducing the array's size by one. If the array is empty, it returns null.

Syntax:

array_shift(array &$array): mixed

Parameters:

  • $array: The input array from which the first element will be removed. This array is passed by reference, meaning the original array will be modified.

Return Value:

  • Returns the value of the removed element. If the array is empty, it returns null.

Example 1: Basic Usage

In this example, we demonstrate how to use array_shift() to remove the first element from an array.

<?php $array = [1, 2, 3, 4, 5]; $firstElement = array_shift($array); echo "Removed Element: " . $firstElement . "\n"; // Output: Removed Element: 1 print_r($array); // Output: Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 ) ?>

Output:

Removed Element: 1 Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 )

In this case, the first element 1 is removed from the array, and the array is modified to contain only [2, 3, 4, 5].

Example 2: Removing from an Associative Array

You can also use array_shift() with associative arrays. The first key-value pair is removed.

<?php $array = ["a" => "apple", "b" => "banana", "c" => "cherry"]; $firstElement = array_shift($array); echo "Removed Element: " . $firstElement . "\n"; // Output: Removed Element: apple print_r($array); // Output: Array ( [b] => banana [c] => cherry ) ?>

Output:

Removed Element: apple Array ( [b] => banana [c] => cherry )

In this example, the first element "apple" is removed, and the keys for the remaining elements remain unchanged.

Example 3: Empty Array

If you call array_shift() on an empty array, it will return null.

<?php $array = []; $removedElement = array_shift($array); echo "Removed Element: " . ($removedElement === null ? 'null' : $removedElement) . "\n"; // Output: Removed Element: null ?>

Output:

Removed Element: null

Here, since the array is empty, the output indicates that no element was removed.

Example 4: Using array_shift in a Loop

You can use array_shift() within a loop to process elements sequentially.

<?php $array = [1, 2, 3, 4, 5]; while ($value = array_shift($array)) { echo "Processing: $value\n"; } ?>

Output:

Processing: 1 Processing: 2 Processing: 3 Processing: 4 Processing: 5

In this example, each element of the array is processed in order until the array is empty.

Practical Usage:

  • array_shift() is useful for:
    • Implementing queue-like structures where you need to process elements in the order they were added.
    • Sequentially accessing and removing elements from the front of an array.
    • Reducing the size of an array dynamically as elements are processed or consumed.

Summary:

  • array_shift(&$array) removes the first element from the specified array and returns that element.
  • The original array is modified by shifting the remaining elements to the left.
  • This function is a valuable tool for managing collections of data in PHP, especially when implementing queue-like behavior or processing elements in a sequence.