I l@ve RuBoard Previous Section Next Section

array_push

int array_push(array array, mixed vars, mixed ...) 

array

Array to which to add the new value(s)

vars

Value to append to array

...

Further values to add

Appends the list of mixed variables onto the given array.

Returns:

Number of elements appended to the array; NULL on failure

Description:

This function appends one or more values to the array given by array . All values beyond the first are appended. Any type of variable can be appended, including mixed or multidimensional arrays. New elements will always have incrementally-increased numeric keys in array , and the new elements will be added in the order in which they are passed to array_push().

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

Warning:

This function directly alters the array passed to it.


Version:

PHP 4

See also:

array_pop() 
array_shift() 
array_unshift() 

Example:

Push values onto an array
$arr = array(10, 20, 30, 40); 
$val = 50; 
echo "Before: \n"; 
print_r($arr); 
array_push($arr, $val); 
echo "Pushing $val onto the array produces:\n"; 
print_r($arr); 
Output: 
Before: 
Array 
(
    [0] => 10 
    [1] => 20 
    [2] => 30 
    [3] => 40 
) 
Pushing 50 onto the array produces: 
Array 
(
    [0] => 10 
    [1] => 20 
    [2] => 30 
    [3] => 40 
    [4] => 50 
) 
    I l@ve RuBoard Previous Section Next Section