Register Login

array_push() function in PHP

Updated Oct 22, 2019

array_push() in PHP

Php array_push() function is used to push(add) one or more new elements onto the end of the array. Due to the addition of these new elements, the length of the array increases by the number of elements pushed.

Syntax

array_push($array, $ele1, $ele2, $ele3....)

Parameter:

Parameter Type Description
$array (mandatory) Array Source array to which we want to add elements
List of elements $ele (mandatory) Integer, String, Float, Array The new elements which we have to insert into the source array

Note: The index value will always be an integer

Return Value:

Php array_push() function returns a modified array with new elements pushed into it from its end.

If the source array has an integer key value, then a numeric key will always be added to the pushed value by the function.

Example:

Input

$classTenSubjects = array(1=>'Mathematics',2=>'Physics',3=>'Chemistry',4=>'English');

//pushing elements

array_push($classTenSubjects,"Hindi","Yoga");

Output

Array ( [1] => Mathematics [2] => Physics [3] => Chemistry [4] => English [5] => Hindi [6] => Yoga )

Php Code Examples:

PHP array_push Basic Program

<?php

#PHP Program to illustrate the working of array_push function 
#Basic Program

$classTenSubjects = array('Mathematics','Physics','Chemistry','English');
echo "Array before push operation : ";
print_r($classTenSubjects);
array_push($classTenSubjects,"Hindi","Yoga");
echo "<br><br>";
echo "Array after push operation : ";
print_r($classTenSubjects);

?>

OUTPUT:

Array before push operation : Array ( [0] => Mathematics [1] => Physics [2] => Chemistry [3] => English )

Array after push operation : Array ( [0] => Mathematics [1] => Physics [2] => Chemistry [3] => English [4] => Hindi [5] => Yoga )

PHP array_push function with Integer Key Value

<?php

#PHP Program to illustrate the working of array_push function
#Insert values with key {with integer key value}

$classTenSubjects = array(1=>'Mathematics',2=>'Physics',3=>'Chemistry',4=>'English');
echo "Array before push operation : ";
print_r($classTenSubjects);
array_push($classTenSubjects,"Hindi","Yoga");
echo "<br><br>";
echo "Array after push operation : ";
print_r($classTenSubjects);

?>

OUTPUT: 

Array before push operation : Array ( [1] => Mathematics [2] => Physics [3] => Chemistry [4] => English )

Array after push operation : Array ( [1] => Mathematics [2] => Physics [3] => Chemistry [4] => English [5] => Hindi [6] => Yoga )

PHP array_push function without Integer Key-Value 

<?php

#PHP Program to illustrate the working of array_push function
#Insert values with key {without integer key value}

$classTenSubjects = array(a=>'Mathematics',b=>'Physics',c=>'Chemistry',d=>'English');
echo "Array before push operation : ";
print_r($classTenSubjects);
array_push($classTenSubjects,"Hindi","Yoga");
echo "<br><br>";
echo "Array after push operation : ";
print_r($classTenSubjects);

?>

OUTPUT:

Array before push operation : Array ( [a] => Mathematics [b] => Physics [c] => Chemistry [d] => English )

Array after push operation : Array ( [a] => Mathematics [b] => Physics [c] => Chemistry [d] => English [0] => Hindi [1] => Yoga )

Push array inside an array using PHP array_push

We can also push a new array as an element in the source array using PHP array_push function

<?php

#PHP Program to illustrate the working of array_push function
#Push array inside an array

$classTenSubjects = array('Mathematics','Physics','Chemistry','English');
echo "Array before push operation : ";
print_r($classTenSubjects);
array_push($classTenSubjects,array('Algebra', 'Trigonometry','Statistics','Dynamics'));
echo "<br><br>";
echo "Array after push operation : ";
print_r($classTenSubjects);

?>

Output

Array before push operation : Array ( [0] => Mathematics [1] => Physics [2] => Chemistry [3] => English )

Array after push operation : Array ( [0] => Mathematics [1] => Physics [2] => Chemistry [3] => English [4] => Array ( [0] => Algebra [1] => Trigonometry [2] => Statistics [3] => Dynamics ) )


×