PHP Array Push

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.
[php] array_push($array,"valu1","valu2");

Example code for PHP Array Push

[php] <?php
$fruits = array("orange", "banana");
array_push($fruits, "apple", "raspberry");

Output array will contain below data

“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
[php] $data[$key] = $value;
[/php] 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
[php] $countries = array(
["IN"] => "India",
["UK"] => "United Kingdom"

$counties["US"] = "United States";

And output array will be

If you have any problem using PHP Array Push function, let me know by using comment form below.

Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: