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.
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 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
nullif 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);
?>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