Menu
Coddy logo textTech

Removing with 'array_pop'

Part of the Logic & Flow section of Coddy's PHP journey — lesson 11 of 68.

The array_pop() function removes the last element from an array and returns that removed value. Unlike array_push() which adds elements, this function shortens your array by one element each time you use it.

Here's how it works:

<?php
$tasks = ["wash dishes", "do laundry", "buy groceries"];
$completedTask = array_pop($tasks);
echo $completedTask;  // Outputs: buy groceries
print_r($tasks);      // Outputs: Array ( [0] => wash dishes [1] => do laundry )
?>

Notice that array_pop() both modifies the original array and returns the removed element. This makes it perfect for scenarios where you need to process items from the end of a list, like completing the last task on a to-do list or removing the most recent item from a stack.

If you try to use array_pop() on an empty array, it will return null without causing an error. This function is particularly useful when you're working with arrays as stacks, where you add items to the end and remove them from the end in a "last in, first out" pattern.

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 tasks in JSON format and a number indicating how many tasks to complete. Read both inputs, convert the JSON string to an array, use array_pop() the specified number of times to remove completed tasks from the end, and print the final array using print_r().

Input format: Two lines - first line contains a JSON array (example: ["task1","task2","task3"]), second line contains the number of tasks to complete (a number)

Expected output: The array after removing the completed tasks, displayed using print_r()

Cheat sheet

The array_pop() function removes the last element from an array and returns that removed value:

<?php
$tasks = ["wash dishes", "do laundry", "buy groceries"];
$completedTask = array_pop($tasks);
echo $completedTask;  // Outputs: buy groceries
print_r($tasks);      // Outputs: Array ( [0] => wash dishes [1] => do laundry )
?>

Key points:

  • Modifies the original array by removing the last element
  • Returns the removed element
  • Returns null if used on an empty array (no error)
  • Useful for "last in, first out" (stack) operations

Try it yourself

<?php
// Read the JSON array input
$jsonInput = fgets(STDIN);
$tasks = (array)json_decode($jsonInput, true);

// Read the number of tasks to complete
$numToComplete = intval(fgets(STDIN));

// TODO: Write your code below
// Use array_pop() to remove the specified number of tasks


// Output the final array
print_r($tasks);
?>
quiz iconTest yourself

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

All lessons in Logic & Flow