PHP array_push() Function
PHP array_push() Function
The array_push() function in PHP pushes one or more elements onto the end of the array. This function increases the length of the array by the number of variables pushed.
Syntax
array_push ( array &$array, mixed& value [, mixed $... ] )
Parameter
array(required)- This parameter represents the input array.
value(required)- This parameter specifies the value to push in the array.
…(optional)- It represents the values to push onto the end of the array.
Return
This function returns the new number of elements in the array.
Example 1
Output
Input array: Array ( [0] => Cheese Cake [1] => Choco Lava [2] => Pie cut ) After pushing 2 elements. New Array: Array ( [0] => Cheese Cake [1] => Choco Lava [2] => Pie cut [3] => Rapsberry Pie [4] => Vani )
Example 2
"Reema", 2=>"Sukla",3=>"Varun", 4=>"Amar"); echo("Input array: \n"); print_r($array); //pushing 2 integer values array_push($array, 45,67,89); echo("\nAfter pushing 2 elements.\nNew Array: "); print_r($array); ?>
Output
Input array: Array ( [1] => Reema [2] => Sukla [3] => Varun [4] => Amar ) After pushing 2 elements. New Array: Array ( [1] => Reema [2] => Sukla [3] => Varun [4] => Amar [5] => 45 [6] => 67 [7] => 89 )
Example 3
"Reema", 2=>"Sukla",3=>"Varun", 4=>"Amar"); echo("Input array: \n"); print_r($array); //pushing the array with null array_push($array, null); echo("\nAfter pushing null element.\nNew Array: "); print_r($array); ?>
Output
Input array: Array ( [1] => Reema [2] => Sukla [3] => Varun [4] => Amar ) After pushing 2 elements. New Array: Array ( [1] => Reema [2] => Sukla [3] => Varun [4] => Amar [5] => )