Pushing an Element into a PHP Array

To add elements to a PHP array, you can use the array_push($array, $val1, $val2, ....) function. The array_push() function adds one or more elements to the end of the array and automatically increases the array's length. The $array parameter refers to the original array to which elements are added. The function will raise an exception if the first argument is not an array. The parameters $val1 and $val2 are the elements we want to push into the array. In this PHP Push Elements to Array example, we add elements to an array using the array_push() function. Click Execute to run the PHP Array Push Example online and see the result.
Pushing an Element into a PHP Array Execute
<?php
$fruits = array("apple", "banana");

array_push($fruits, "mango", "orange");

print_r($fruits);
?>
Updated: Viewed: 9139 times

What is PHP?

PHP is an open-source scripting language used for web development that can be embedded in HTML code. PHP is known for its flexibility and large developer community. PHP can run on many operating systems, including Windows, Linux, and macOS, and supports multiple databases such as MySQL, PostgreSQL, and SQLite.

What is an array in PHP?

In PHP, an array is a variable used to store a collection of data elements identified by a unique key or index (the key can be a string or a number). PHP has two types of arrays: indexed arrays and associative arrays. The PHP provides several built-in functions to manipulate arrays, such as sorting, searching, checking for element existence, splitting a string to array, converting arrays to string or JSON, and retrieving array length.

PHP Array Push Syntax

The following is the syntax of the array_push() function, which inserts one or more elements at the end of an array:

PHP array_push() Syntax
array_push(array, val1, val2, ...)

Where:
  • array: defines an input array
  • val (Optional): one or more elements that we want to put in the array. In the syntax above, the list looks like this: $val1, $val2, ….
PHP array_push() Example
<?php
$fruits = array("apple", "banana");

array_push($fruits, "mango", "orange");

print_r($fruits);
?>

#output: Array
#(
#    [0] => apple
#    [1] => banana
#    [2] => mango
#    [3] => orange
#)

PHP Array Push Examples

The following are examples of pushing an element into a PHP array:

Pushing one element into an array

The following is an example of pushing a single element into an array using the array_push() method:

PHP Push One Element into an Array
<?php
$fruits = array("apple", "banana");

array_push($fruits, "mango");

print_r($fruits);
?>

#output: Array
#(
#    [0] => apple
#    [1] => banana
#    [2] => mango
#)

Pushing multiple elements into an array

The following is an example of pushing multiple elements into an array using the array_push() method:

PHP Push Multiple Elements into an Array
<?php
$fruits = array("cherry", "mango");

array_push($fruits, "melon", "grape");

print_r($fruits);
?>

#output: Array
#(
#    [0] => cherry
#    [1] => mango
#    [2] => melon
#    [3] => grape
#)

Pushing an element into a nested array

The following is an example of pushing an element into a nested array using the array_push() method:

PHP
<?php
$arr = array(
    "fruits" => array("apple", "cherry"),
    "veggies" => array("carrot", "onion")
);

array_push($arr["fruits"], "orange");

print_r($arr);
?>

#output: Array
#(
#   [fruits] => Array
#      (
#          [0] => apple
#          [1] => cherry
#          [2] => orange
#      )
#
#    [veggies] => Array
#      (
#          [0] => carrot
#          [1] => onion
#      )
#
#)

See also