PHP Array Push
PHP Array Push function is used to Push one or more elements onto the end of array. You can add one value or any number of values of values to an array at the end using Array Push PHP function. Array Push accepts two arguments first argument is the array to which you want to add other values. And second argument is value, you can add any number of values. Here is the example for how to use Array Push PHP function.
Syntax of PHP Array Push
Basic syntax of array push function, $array is the array to which you want to add values at the end. value1 & value2 are the values for adding at the end.
Example code for PHP Array Push
<?php $fruits = array("orange", "banana"); array_push($fruits, "apple", "raspberry"); print_r($fruits); ?>
Output array will contain below data
 => "orange"
 => "banana"
 => "apple"
 => "raspberry"
“apple” and “raspberry” are added to the end of main array.
Note: If you are using array_push it will creates key, If you want to insert a “$key” => “$value” pair into an array, it can be done using below code
$data[$key] = $value;
It is not necessary to use array_push.
Sample code for above example is if you an array of countries list and want to add another country to that array, see code example below
$countries = array( ["IN"] => "India", ["UK"] => "United Kingdom" ); $counties["US"] = "United States";
And output array will be
[IN] => "India"
[UK] => "United Kingdom"
[US] => "United States"
If you have any problem using PHP Array Push function, let me know by using comment form below.
Incoming search terms:
- php array push