Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


The PHP Array array_push() function is used to insert one or more elements to the end of an array. The function treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed.

Syntax

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

Parameters

array Required. Specify an array.
value1 Optional. Specify the value to add.
value2 Optional. Specify the value to add.

Return Value

Returns the new number of elements in the array.

Exceptions

NA.

Example:

In below example, array_push() function is used to add elements in the given array.

<?php
$Arr = array(10, 20);

//add single element in the array
array_push($Arr, 30);

//add multiple elements in the array
array_push($Arr, 40, 50);

print_r($Arr);
?>

The output of the above code will be:

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Example:

In the below example shows the usage of array_push() function with an associative array.

<?php
$Arr = array("A"=>10, "B"=>20);

//add elements in the array
array_push($Arr, 30, 40);

print_r($Arr);
?>

The output of the above code will be:

Array
(
    [A] => 10
    [B] => 20
    [0] => 30
    [1] => 40
)

❮ PHP Array functions