Menu
Coddy logo textTech

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You 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 null if 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);
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow