Removing with 'array_shift'
Part of the Logic & Flow section of Coddy's PHP journey — lesson 13 of 68.
The array_shift() function removes the first element from an array and returns that removed value. This is the opposite of array_unshift(), which adds elements to the beginning of an array.
Here's how it works:
<?php
$queue = ["Alice", "Bob", "Charlie"];
$firstPerson = array_shift($queue);
echo $firstPerson; // Outputs: Alice
print_r($queue); // Outputs: Array ( [0] => Bob [1] => Charlie )
?>Notice that array_shift() both modifies the original array and returns the removed element. After removing the first element, all remaining elements automatically shift down to fill the gap—"Bob" moves from index 1 to index 0, and "Charlie" moves from index 2 to index 1.
This re-indexing happens automatically, ensuring your array maintains proper numeric sequence. If you use array_shift() on an empty array, it returns null without causing an error.
This function is particularly useful for implementing queues, where you process items in a "first in, first out" order—like handling customers in line or processing tasks in the order they were received.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou will receive two inputs: an initial array of customers waiting in line in JSON format and a number indicating how many customers to serve. Read both inputs, convert the JSON string to an array, use array_shift() the specified number of times to remove served customers from the front of the line, and print the final array using print_r().
Input format: Two lines - first line contains a JSON array (example: ["Alice","Bob","Charlie","Diana"]), second line contains the number of customers to serve (a number)
Expected output: The array after removing the served customers from the front, displayed using print_r()
Cheat sheet
The array_shift() function removes the first element from an array and returns that removed value:
<?php
$queue = ["Alice", "Bob", "Charlie"];
$firstPerson = array_shift($queue);
echo $firstPerson; // Outputs: Alice
print_r($queue); // Outputs: Array ( [0] => Bob [1] => Charlie )
?>Key points:
- Modifies the original array and returns the removed element
- Remaining elements automatically re-index (shift down)
- Returns
nullif used on an empty array - Useful for implementing "first in, first out" queues
Try it yourself
<?php
// Read the JSON array of customers
$jsonInput = fgets(STDIN);
$customers = (array)json_decode($jsonInput, true);
// Read the number of customers to serve
$numToServe = intval(fgets(STDIN));
// TODO: Write your code below
// Use array_shift() to remove customers from the front of the line
// Output the final array
print_r($customers);
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting