PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




array_pop

Name

array_pop — Returns and deletes the last element of an array.

Synopsis

mixed array_pop(array);
array array: Array from which to be popped

Returns

Last element of the given array; NULL on failure or if the array is empty

Description

array_pop() returns the last value of array and deletes that element from the array. This also works for multidimensional arrays, so if the last element happens to be an array, that array is returned and deleted from array .

This function can be used with array_push() to treat arrays as stacks.

Warning

This function directly alters the array passed to it.

Version

PHP 4

Example

Example 27. Pop elements from various arrays

$a1 = array('one', 'two', 'three', 'four');
$a2 = array('jan' => '01', 'feb' => '02', 'mar' => '03');
$a3 = array('one', 'two', 'three' => array('a', 'b', 'c'));
echo "First array before popping:\n";
print_r($a1);
$popped = array_pop($a1);
echo "First array after popping the value '$popped':\n";
print_r($a1);

echo "Second array before popping:\n";
print_r($a2);
$popped = array_pop($a2);
echo "Second array after popping the value '$popped':\n";
print_r($a2);

echo "Third array before popping:\n";
print_r($a3);
$popped = array_pop($a3);
echo "Third array after popping the final array:\n";
print_r($a3);

echo "Array popped from the end of the third array:\n";
print_r($popped);
Output:
First array before popping:
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)
First array after popping the value 'four':
Array
(
    [0] => one
    [1] => two
    [2] => three
)
Second array before popping:
Array
(
    [jan] => 01
    [feb] => 02
    [mar] => 03
)
Second array after popping the value '03':
Array
(
    [jan] => 01
    [feb] => 02
)
Third array before popping:
Array
(
    [0] => one
    [1] => two
    [three] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)
Third array after popping the final array:
Array
(
    [0] => one
    [1] => two
)
Array popped from the end of the third array:
Array
(
    [0] => a
    [1] => b
    [2] => c
)



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.