Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. array_push() / array_pop() / array_shift() / array_unshift()

array_push() / array_pop() / array_shift() / array_unshift()

Since: PHP 4(2000)

Functions for adding and removing elements at the beginning or end of an array. Use these when you need stack or queue style data manipulation.

Syntax

array_push(array, value1, value2, ...);

array_pop(array);

array_shift(array);

// Adds one or more elements to the beginning of an array.
array_unshift(array, value1, value2, ...);

Function List

FunctionDescription
array_push($array, $value, ...)Adds one or more elements to the end of an array. Returns the new number of elements in the array.
array_pop($array)Removes and returns the last element of an array, shortening the array by one.
array_shift($array)Removes and returns the first element of an array. The remaining elements are reindexed starting from zero.
array_unshift($array, $value, ...)Adds one or more elements to the beginning of an array. Returns the new number of elements in the array.

Sample Code

sample_array_push.php
<?php
$members = ['Okabe Rintaro', 'Makise Kurisu', 'Shiina Mayuri'];

// Add elements to the end.
array_push($members, 'Hashida Itaru', 'Amane Suzuha');
print_r($members);

$last = array_pop($members);
echo $last . "\n";

$first = array_shift($members);
echo $first . "\n";

// Add elements to the beginning.
array_unshift($members, 'Faris Nyannyan', 'Kiryu Moeka');
print_r($members);

// You can also use $members[] to append to the end.
$members[] = 'Urushibara Ruka';
print_r($members);

Running the code produces the following output:

php sample_array_push.php
Array
(
    [0] => Okabe Rintaro
    [1] => Makise Kurisu
    [2] => Shiina Mayuri
    [3] => Hashida Itaru
    [4] => Amane Suzuha
)
Amane Suzuha
Okabe Rintaro
Array
(
    [0] => Faris Nyannyan
    [1] => Kiryu Moeka
    [2] => Makise Kurisu
    [3] => Shiina Mayuri
    [4] => Hashida Itaru
)
Array
(
    [0] => Faris Nyannyan
    [1] => Kiryu Moeka
    [2] => Makise Kurisu
    [3] => Shiina Mayuri
    [4] => Hashida Itaru
    [5] => Urushibara Ruka
)

Notes

array_push() and array_pop() operate on the end of an array, while array_unshift() and array_shift() operate on the beginning. By combining these, you can implement stack and queue behavior.

When you only need to append a single element, the $array[] = $value syntax is simpler and faster. array_push() is used when you want to add multiple values at once.

When array_shift() is called, the remaining numeric keys are re-indexed starting from zero. String keys are preserved, so be aware of key changes when using it with associative arrays. To manipulate elements at an arbitrary position, array_splice() can be used.

array_shift re-indexes numeric keys

After calling array_shift(), the remaining numeric keys are re-indexed starting from zero. String keys remain unchanged. Code that depends on specific numeric index values may behave unexpectedly after this operation.

sample.php
<?php
$queue = ['Okabe Rintaro', 'Makise Kurisu', 'Shiina Mayuri', 'Hashida Itaru'];

echo "Before: ";
print_r(array_keys($queue));

$first = array_shift($queue);
echo "Removed: " . $first . "\n";

echo "After: ";
print_r(array_keys($queue));

Running the code produces the following output:

php sample.php
Before: Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
)
Removed: Okabe Rintaro
After: Array
(
    [0] => 0
    [1] => 1
    [2] => 2
)

String keys in associative arrays are preserved.

sample_02.php
<?php
$assoc = ['okabe' => 'Okabe Rintaro', 'kurisu' => 'Makise Kurisu', 'mayuri' => 'Shiina Mayuri'];
$first = array_shift($assoc);
echo "Removed: " . $first . "\n";
print_r($assoc);

Running the code produces the following output:

php sample_02.php
Removed: Okabe Rintaro
Array
(
    [kurisu] => Makise Kurisu
    [mayuri] => Shiina Mayuri
)

$array[] = is faster than array_push

When adding a single element, the $array[] = $value syntax avoids the function call overhead and is faster. array_push() is used when adding multiple values at once.

sample_03.php
<?php
$log = [];

// Add with array_push
array_push($log, 'Okabe arrived at the lab');

// Add with $array[] (faster for single-element additions)
$log[] = 'Kurisu started the experiment';

print_r($log);

// array_push is convenient for adding multiple elements at once
array_push($log, 'Mayuri brought snacks', 'Itaru booted up the PC');
print_r($log);

Running the code produces the following output:

php sample_03.php
Array
(
    [0] => Okabe arrived at the lab
    [1] => Kurisu started the experiment
)
Array
(
    [0] => Okabe arrived at the lab
    [1] => Kurisu started the experiment
    [2] => Mayuri brought snacks
    [3] => Itaru booted up the PC
)

Arrays are passed by reference

Functions like array_push() and array_pop() receive the array by reference and modify it directly. When calling them inside a function, pass the array normally — the original array will be modified. There is no need to declare the parameter with & just for these array functions.

sample_04.php
<?php
function add_member(&$list, $name) {
    // Received by reference — modifies the original
    array_push($list, $name);
}

function add_member_normal($list, $name) {
    // Received by value — does not affect the original
    array_push($list, $name);
}

$members = ['Okabe Rintaro', 'Makise Kurisu'];

add_member($members, 'Shiina Mayuri');
echo "After reference pass: " . count($members) . " members\n";

add_member_normal($members, 'Hashida Itaru');
echo "After value pass: " . count($members) . " members\n";

Running the code produces the following output:

php sample_04.php
After reference pass: 3 members
After value pass: 3 members

Practice Patterns

Using as a stack (LIFO)

Uses an array as a stack (last in, first out). Push with array_push() and pop with array_pop().

sample_05.php
<?php
$history = [];

// Push time eras visited by the time machine
array_push($history, '2011');
array_push($history, '2010');
array_push($history, '1975');

echo "Current era: " . end($history) . "\n";

// Return from the most recently visited era (LIFO)
while (!empty($history)) {
    $era = array_pop($history);
    echo "Returned from: " . $era . "\n";
}

Running the code produces the following output:

php sample_05.php
Current era: 1975
Returned from: 1975
Returned from: 2010
Returned from: 2011

Using as a queue (FIFO)

Uses an array as a queue (first in, first out). Enqueue with array_push() and dequeue with array_shift().

sample_06.php
<?php
$queue = [];

// Add tasks to the queue
array_push($queue, "Check Okabe's email");
array_push($queue, "Kurisu's experiment report");
array_push($queue, "Prepare for Mayuri's birthday");

// Process tasks in order (FIFO)
while (!empty($queue)) {
    $task = array_shift($queue);
    echo "Processing: " . $task . "\n";
}

Running the code produces the following output:

php sample_06.php
Processing: Check Okabe's email
Processing: Kurisu's experiment report
Processing: Prepare for Mayuri's birthday

If you find any errors or copyright issues, please .