How to Add Elements to an Array in PHP

09/05/2021

Contents

In this article, you will learn how to add elements to an array in PHP.

PHP array_push() Function

The array_push() function in PHP is used to add one or more elements to the end of an array. The function takes two or more parameters, the first parameter being the array to which elements are to be added and the rest of the parameters are the elements to be added to the array.

The array_push() function returns the new number of elements in the array after the elements have been added.

Here is an example of how the array_push() function can be used:

<?php
  $fruits = array("apple", "banana");
  $count = array_push($fruits, "orange", "mango");
  // $fruits = array("apple", "banana", "orange", "mango")
  // $count = 4
?>

In this example, the array_push() function is used to add “orange” and “mango” to the end of the $fruits array. The function returns 4, which is the new number of elements in the array after the elements have been added.

It’s important to note that array_push() function modifies the original array. If you don’t want to modify the original array, you can use the array_concat() function.

PHP $array[] Syntax

In PHP, the $array[] syntax is used to add an element to the end of an array. This syntax is a shorthand way of calling the array_push() function. It allows you to add an element to an array without having to specify the array name multiple times.

Here’s an example of how you can use the $array[] syntax:

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

This code will add “orange” and “mango” to the end of the $fruits array, just like the previous example.

You can also use this syntax to add an element to an associative array. Like this:

<?php
  $person = ["name" => "John", "age" => 25];
  $person["location"] = "New York";
?>

This code adds an element “location” with the value “New York” to the $person array.

In both cases, the $array[] syntax is used to add elements to the array, and it automatically increments the key of the new element.

It’s worth noting that when using the [] operator, if the array is empty, it will automatically start with index 0, and keep incrementing the index for each new element added.

PHP array_unshift() Function

The array_unshift() function in PHP is used to add one or more elements to the beginning of an array. The function takes two or more parameters, the first parameter being the array to which elements are to be added and the rest of the parameters are the elements to be added to the array.

The array_unshift() function returns the new number of elements in the array after the elements have been added.

Here is an example of how the array_unshift() function can be used:

<?php
  $fruits = array("apple", "banana");
  $count = array_unshift($fruits, "orange", "mango");
  // $fruits = array("orange", "mango", "apple", "banana")
  // $count = 4
?>

In this example, the array_unshift() function is used to add “orange” and “mango” to the beginning of the $fruits array. The function returns 4, which is the new number of elements in the array after the elements have been added.

It’s important to note that array_unshift() function modifies the original array. If you don’t want to modify the original array, you can use the array_merge() function.

PHP array_merge() Function

The array_merge() function in PHP is used to merge one or more arrays into a single array. The function takes any number of arrays as parameters and returns a new array that contains all the elements from the original arrays.

Here is an example of how the array_merge() function can be used:

<?php
  $fruits1 = array("apple", "banana");
  $fruits2 = array("orange", "mango");
  $fruits3 = array_merge($fruits1, $fruits2);
  // $fruits3 = array("apple", "banana", "orange", "mango")
?>

In this example, the array_merge() function is used to combine the elements of the $fruits1 and $fruits2 arrays into a single array, $fruits3.

It’s important to note that, if the arrays being merged have the same string keys, then the later value for that key will overwrite the previous one. In other words, when merging arrays with identical string keys, the last array will overwrite the previous ones.

<?php
  $array1 = ["a" => "red", "b" => "green"];
  $array2 = ["c" => "blue", "a" => "yellow"];
  $result = array_merge($array1, $array2);
  // $result = ["a" => "yellow", "b" => "green", "c" => "blue"]
?>

You can also use the + operator to merge arrays, which works in a similar way as the array_merge() function.

<?php
  $fruits1 = ["apple", "banana"];
  $fruits2 = ["orange", "mango"];
  $fruits = $fruits1 + $fruits2;
?>

Also, since PHP 7.4, you can use the “…” operator (the splat operator) to merge multiple arrays into one.

<?php
  $fruits1 = ["apple", "banana"];
  $fruits2 = ["orange", "mango"];
  $fruits3 = ["strawberry", "kiwi"];
  $fruits = [...$fruits1, ...$fruits2, ...$fruits3];
?>

This will merge all the elements of $fruits1, $fruits2 and $fruits3 into one array $fruits.