Understanding PHP's array_shift() Function: Syntax, Parameters, Return Values, and Practical Examples

This article explains PHP's array_shift() function, covering its syntax, parameters, return value, and demonstrating its use with examples for basic arrays, empty arrays, and associative arrays; it also shows how the function modifies the original array and what it returns when the array is empty.

php Courses
php Courses
php Courses
Understanding PHP's array_shift() Function: Syntax, Parameters, Return Values, and Practical Examples

PHP is a popular scripting language widely used for web development, offering many built‑in functions for array manipulation, including the useful array_shift() function.

The array_shift() function removes the first element from an array, returns its value, reduces the array length by one, and reindexes the remaining elements, making it ideal for queue or list handling.

Syntax: array_shift(array $array): mixed Parameter: $array – the array to operate on.

Return value:

mixed – the value of the removed first element, or NULL if the array is empty.

Example 1 – Basic usage:

$fruits = array("apple", "banana", "orange");
$firstFruit = array_shift($fruits);
echo "First fruit: " . $firstFruit; // Output: First fruit: apple
print_r($fruits); // Output: Array ( [0] => banana [1] => orange )

This example demonstrates removing "apple" from $fruits, leaving "banana" and "orange" with updated indexes.

Example 2 – Handling an empty array:

$emptyArray = array();
$firstElement = array_shift($emptyArray);
echo "First element: " . $firstElement; // Output: First element:
print_r($emptyArray); // Output: Array ( )

When the array is empty, array_shift() returns NULL and the array remains unchanged.

Example 3 – Working with an associative array:

$student = array(
    "name" => "John",
    "age" => 20,
    "gender" => "male"
);
$firstAttribute = array_shift($student);
echo "First attribute: " . $firstAttribute; // Output: First attribute: John
print_r($student); // Output: Array ( [age] => 20 [gender] => male )

For associative arrays, the function returns the value of the first key ("John") and removes that entry, leaving the remaining key‑value pairs.

Summary: The array_shift() function is a versatile tool for PHP developers to efficiently remove and retrieve the first element of both indexed and associative arrays, useful in queue processing and other array‑handling scenarios.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

php-functionsarray_shiftarray-manipulation
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.