Site Search
Homepage of Otaku No Zoku
Complete Archives of Otaku No Zoku
About Otaku No Zoku
Subscribe to Otaku No Zoku
Bookmark Otaku No Zoku

PHP array_pop() Function :

PHP provides incredibly rich and diverse functionality for manipulating arrays. The word “array” and the data type array() in PHP is a bit of a misnomer because the built-in functionality of PHP  is quite capable of treating arrays as though they were many other data structures from simple arrays such as regular single-dimension arrays, regular, i.e. square, multi-dimensional arrays, through to ragged arrays, PHP arrays can also be treated as single and doubly linked lists, stacks (Last-in, First-out LIFO), queues (First-in, First-out FIFO), circular lists, deques (double-ended queues), and so forth. Any linear, single dimension data structure can be represented in PHP by an array(). When you start making use of trees or tries, or other branching data structures, then you have to get fancy, but that’s an article for another time.

Two pieces of functionality that enable PHP to treat an array as a data structure similar to a stack are the array_push() and the array_pop() functions. This article talks about the array_pop() function.

The PHP function array_pop(), if you know anything about the stack data structure, makes an array() data type work just like a stack, taking items from the end of the array() and reducing the size of the array() by one, i.e. the array_pop() function pops an element off the end of the array, just like as though you had popped a item off of the top of a stack.

If the array() is empty, and you attempt to pop off an item from the array, NULL is returned. Additionally, if you attempt to pop an item off of a data object, such as an integer, that is not a PHP array, you get the NULL return value (always check your return values!) and an additional warning in the console or debug output.

You should also be aware that PHP maintains an internal array pointer used for other array manipulations, such as iterating over an array, and that internal pointer is reset after you perform array_pop() so you should not rely on the previous value of the internal array pointer if you need to perform other array manipulations that depend upon it.

PHP array_pop() Function Definition

mixed array_pop(array &$source_array)

source_array

Required

The source array to retrieve the value from.

Returns the last element of source_array, or NULL if source_array is empty or the specified source_array is not actually an array() data type.

Popping An Element Off The Stack

This example shows how to pop an element off of a stack by treating a PHP array() data type as a stack data structure.

Steps

  1. Allocate a small array of integers.
  2. Use the array_pop() function to retrieve the last element in the array.
  3. Print the popped value.
  4. Print the now shorter array() of integers after the array_pop() operation has been performed.

The Source Code

<?php

// allocate a small array of integers
$stack = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
		
// pop an element 
$lifo_number = array_pop($stack);
		
// output the element popped off the top of the stack
var_dump($lifo_number);
		
// output each number remaining in the stack
var_dump($stack);

?>

Program Output

int(10)
array(9) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) }

Results

The first line of the program output shows the value that has been popped off the top of the stack, i.e. the end of the array(), which in this case is the integer 10. Look at the array() declaration in the first actual statement of the code. Notice how the numbers run from 1 through 10 when the array is declared? So we took the last number in the array, in this case, the number 10 — which also by coincidence happens to the be tenth element in the array but that is neither here nor there – and popped it off the end of the array, i.e. we treated the array as a stack-like data structure and popped the item off of the top of the stack.

The second line of output shows the now shorter array, with only nine elements in it, containing the digits 1 through 9. The tenth element has been popped off the end, shortening the array() by one element. You could continue this array_pop() operation until the array no longer has any elements in it, at which time the next value you receive will be NULL.

Summary

Being able to treat PHP arrays as many other data structures opens up a huge number of programming opportunities and enables very simple solutions to some quite complex problems without really having to think too hard about what type of data structure should be declared. Because PHP arrays are so versatile you don’t have to worry about converting an array in to a list or stack and back again like you do in many other languages. PHP doesn’t even have a stack data type and you don’t really need one because arrays will do the job just fine thankyouverymuch. PHP does provide a list data structure, but that’s for a very specific reason, which I will cover in another article.

Liked This Post?

Subscribe to the RSS feed or follow me on Twitter to stay up to date!