PHP | array_push() Function
This inbuilt function of PHP is used to push new elements into an array. We can push one or more than one element into the array and these elements gets inserted to the end of the array and because of the pushed elements into the array, the length of the array also gets incremented by the number of elements pushed into the array.
Syntax:
array_push($array, $val1, $val2, $val3....)
Parameters:
The function can take multiple parameters, depending on the number of elements we want to push into the array. We can classify the parameters in two categories as shown below:
- $array: This parameter refers to the original array we want to operate upon.
- List of values: This parameter refers to the list of elements separated by commas we want to push into the array. In the above syntax the list of values to be pushed is $val1, $val2, $val3….
Return Value: This function returns the modified array, with all the elements pushed to the end of the array.
Note: If the array has a key, value pair, then the method will always add a numeric key to the pushed value.
Examples:
Input : $array = (1=>"ram", 2=>"krishna", 3=>"aakash")
$val1 = "rohan", $val2 = "rajeeb", $val3 = "saniya"
Output :
Array
(
[1] => ram
[2] => krishna
[3] => aakash
[4] => rohan
[5] => rajeeb
[6] => saniya
)
Input : $array = ("ram", "krishna", "aakash");
$val1 = "rohan", $val2 = "rajeeb", $val3 = "saniya"
Output :
Array
(
[0] => ram
[1] => krishna
[2] => aakash
[3] => rohan
[4] => rajeeb
[5] => saniya
)
Below programs illustrate the array_push() function in PHP:
- In the below program the array_push() function is used to push new elements in an array with no keys.
<?php// PHP code to illustrate the use of array_push()// Input array$array=array("ram","krishna","aakash");// elements to push$a1="rohan";$a2="rajeeb";$a3="saniya";// array after pushing new elementsprint_r(array_push($array,$a1,$a2,$a3));?>chevron_rightfilter_noneOutput:
Array ( [0] => ram [1] => krishna [2] => aakash [3] => rohan [4] => rajeeb [5] => saniya ) -
In the below program, we will understand how the array_push() function works with an array having a already defined key_value pair.
<?php// PHP code to illustrate the use of array_push()// Input Array$array=array(1=>"ram", 2=>"krishna", 3=>"aakash");// Elements to push$a1="rohan";$a2="rajeeb";$a3="saniya";// Array after pushing new elementsprint_r(array_push($array,$a1,$a2,$a3));?>chevron_rightfilter_noneOutput:
Array ( [1] => ram [2] => krishna [3] => aakash [4] => rohan [5] => rajeeb [6] => saniya )
Reference:
http://php.net/manual/en/function.array-push.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- D3.js | d3.map.set() Function
- PHP | max( ) Function
- PHP | exp() Function
- D3.js | d3.min() function
- PHP | Ds\Map get() Function
- CSS | rgb() Function
- PHP | min( ) Function
- p5.js | box() Function
- p5.js | value() Function
- D3.js | d3.rgb() Function
- p5.js | int() function
- D3.js | d3.hcl() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



