PHP » Arrays » array_push()

Syntax:
int array_push(array array, mixed element1 [, mixed element2, ...])
array
The array to which elements should be added.
element1
First element to add to the end of the array.
element2
Second element to add to the end of the array.

Adds elements to the end of an array.

With array_push(), one or more elements can be added to the end of an array. The number of elements that were added is returned.

An array can be used as a stack by using array_push() and array_pop() together.

Examples

Code:
<?php

$array1[] = "orange";
array_push($array1, "apple", "banana", "pear");

for ($i = 0; $i < 4; $i++) {
   print array_pop($array1) . " " . sizeof($array1) . "<br>";
}

?>
Output:
pear 3
banana 2
apple 1
orange 0
Explanation:

A few elements are pushed into an array with array_push(). Each element is then printed as it is popped, together with the size of the remaining array. Note that the elements are printed in reverse order.

See Also: