Skip to content

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 value. This can be useful for a variety of purposes, including removing unnecessary elements, updating arrays, and processing data.

In this article, we will take a closer look at the array_shift function, exploring its syntax, usage, and examples. Whether you are a beginner or an advanced PHP developer, this guide will provide you with the information you need to effectively use this powerful function.

Syntax of the array_shift Function

The syntax for the array_shift function is relatively simple. It requires just one argument: the array that you want to manipulate. Here is an example of the basic syntax:

PHP array_shift function syntax

php
array_shift(array);

It's important to note that the array_shift function modifies the original array, so you don't need to assign the result to a new variable.

Usage of the array_shift Function

The array_shift function is typically used to remove the first element from an array. This can be useful when you need to process an array one element at a time, or when you want to remove unwanted elements from the beginning of an array.

For example, consider the following array:

Define an array

php
<?php

$colors = array("red", "green", "blue");

array_shift($colors);

print_r($colors);

?>

If you want to remove the first element (red), you can use the array_shift function like this:

The result will be:


console
Array
(
    [0] => green
    [1] => blue
)

It's worth mentioning that the array_shift function returns the value of the removed element, so you can store the result in a variable if needed:

PHP get en element after array_shift

php
$first_color = array_shift($colors);

The result will be:

Example of array_shift function usage

php
<?php

$colors = array("red", "green", "blue");
$first_color = array_shift($colors);

print_r($first_color);
print_r($colors);

?>

Examples of the array_shift Function

To better understand the use of the array_shift function, let's take a look at a few examples.

Example 1: Removing the First Element of an Array

In this example, we will remove the first element of an array using the array_shift function. Consider the following array:

PHP define an array

php
$colors = array("red", "green", "blue");

We can remove the first element using the array_shift function:

PHP using array_shift

php
array_shift($colors);

The result will be:


console
$colors = array("green", "blue");

Example 2: Processing an Array One Element at a Time

In this example, we will use the array_shift function to process an array one element at a time. Consider the following array:

Define an array

php
$colors = array("red", "green", "blue");

We can use the array_shift function in a loop to process each element:

PHP loop array and use array_shift

php
<?php

$colors = array("red", "green", "blue");
while ($color = array_shift($colors)) {
    echo $color . "\n";
}

The result will be:


console
red
green

In this example, the loop continues until all elements have been processed, and the array_shift function returns false when the array is empty.

Example 3: Removing Unwanted Elements from the Beginning of an Array

In this example, we will use the array_shift function to remove unwanted elements from the beginning of an array. Consider the following array:

Exaple of using array_shift function to remove unwanted elements from the beginning of an array

php
<?php

$colors = array("red", "green", "blue", "yellow", "orange");

// We can use the `array_shift` function to remove the first three elements:

array_shift($colors);
array_shift($colors);
array_shift($colors);

print_r($colors);

/*
Array
(
    [0] => yellow
    [1] => orange
)
*/
?>

Conclusion

The array_shift function in PHP is a powerful tool for manipulating arrays. Whether you need to remove the first element, process an array one element at a time, or remove unwanted elements from the beginning of an array, the array_shift function can help.

We hope this article has provided you with a comprehensive understanding of the array_shift function and its usage. With this knowledge, you can now effectively use this function in your own PHP projects.

Practice

What does the array_shift() function in PHP do?

Dual-run preview — compare with live Symfony routes.