How to add to an array in PHP

There are several methods to add to an array in PHP. These methods differ in terms of adding elements at different positions in an array. I have been used to array_push and use it most of the time to add multiple elements at one go.

By the end of this article, you’ll learn different ways of adding elements to an array.

Option 1: Square Bracket Syntax

Square bracket syntax is probably the most well-known method to add to an array in PHP. You can add individual elements either by an integer or a string index passed to the square brackets. You can also leave the square brackets empty and PHP will automatically add them to the next available index.

This method is preferable when you need to add a single element. It avoids the overhead of calling a function. However, for multiple elements, this syntax could be daunting.

<?php
 
//Array with three elements.
$fruits = array('Apple','Orange','Mango');
 
//Square bracket syntax: Adding elements by an integer index.
$fruits[3] = "Watermelon";
 
//Square bracket syntax: Adding elements by a string index.
$fruits['Vegetable'] = 'Carrot';
 
//Square bracket syntax: No index
$fruits[] = 'Avocado';
 
//Output the array to console.
print_r($fruits);
 
/* Output
Array
(
    [0] => Apple
    [1] => Orange        
    [2] => Mango
    [3] => Watermelon    
    [Vegetable] => Carrot
    [4] => Avocado       
)
*/
?>

The code above covers all the possibilities to add to an array in PHP using this syntax. A remarkable feature is that PHP won’t throw an error even if you skip an index. The following code demonstrates this by skipping index 3.

<?php
//Array with three elements.
$fruits = array('Apple','Orange','Mango');
 
//Square bracket syntax: Adding elements by an integer index (Skipping index 3).
$fruits[4] = "Watermelon";
 
//Output the array to console.
print_r($fruits);
 
/* Output
Array
(
    [0] => Apple
    [1] => Orange
    [2] => Mango
    [4] => Watermelon
)
*/
?>

PHP is smart, isn’t it? Next, we’ll see array_push which is a PHP function that can add one or more elements to an array.

Option 2: Add to end of array with array push

PHP’s array_push function adds elements to the end of the array. The great thing about array_push is that you can add any number of elements to the end of an array. I prefer this function whenever I need to add multiple elements at one go. It’s pretty simple to use.

<?php
 
//Array with three elements.
$fruits = array('Apple','Orange','Mango');
 
//array_push: Adding three elements to the end of the $fruits array.
array_push($fruits,'Watermelon','Avocado','Strawberries');
 
 
//Output the array to console.
print_r($fruits);
 
/*
Array
(
    [0] => Apple       
    [1] => Orange      
    [2] => Mango       
    [3] => Watermelon  
    [4] => Avocado     
    [5] => Strawberries
)
*/
 
?>

We see how easily we add three items at once through array_push. The only concern using array_push is that it adds by integer index only.

Next, we’ll see array_unshift which is opposite to array_push.

Option 3: Add to beginning of an array with array_unshift

While array_push adds to the end of an array, PHP’s array_unshift adds elements to the start of an array. You can add any number of elements, and the array will reindex the elements every time. Let’s demonstrate this through code.

<?php
 
//Array with three elements.
$fruits = array('Apple','Orange','Mango');
 
//array_unshift: Adding three elements to the start of the $fruits array.
array_unshift($fruits,'Watermelon','Avocado','Strawberries');
 
 
//Output the array to console.
print_r($fruits);
 
/*Output
Array
(
    [0] => Watermelon  
    [1] => Avocado     
    [2] => Strawberries
    [3] => Apple       
    [4] => Orange      
    [5] => Mango       
)
*/
 
?>

See how neatly it adds the elements and reindexes the array. Next, we see array_splice which can add elements to an array at any specified index.

Option 4: Replace and add new items to array with array splice

PHP’s array_splice is mainly used for removing and replacing elements in an array. However, there is a neat trick if you want to add to an array using this function. First, let’s understand the function and its arguments as specified in the official documentation.

array_splice(array,start,length,value)

Arguments

  • array –  Input array (Required)
  • start –  The offset value for specifying where the function should start replacing values.
  • length –  Specifies the number of elements to be replaced. 
  • value A replacement of the removed values from the array.

Passing zero to the length argument doesn’t remove elements from the array but adds at the specified start value. The array_splice function is robust because it can add elements at any position in an array, unlike array_push and array_unshift.

1. Adding at the middle of an array

The code here adds at the middle of an array at index 2. We specify an offset value of 2 and the function adds the elements at that position.

<?php
 
//Array with four elements.
$fruits = array('Apple','Orange','Mango','Watermelon');
 
//array_splice: Adding two elements to the $fruits array.
array_splice($fruits,2,0,['Avocado','Strawberries']);
 
 
//Output the array to console.
print_r($fruits);
 
/*Output
Array
(
    [0] => Apple       
    [1] => Orange      
    [2] => Avocado     
    [3] => Strawberries
    [4] => Mango       
    [5] => Watermelon  
)
*/
 
?>

2. Adding at the start of an array

That’s how we can add to the start of an array using array_splice. A start value of zero sets the offset to the beginning of the array.

<?php
 
//Array with four elements.
$fruits = array('Apple','Orange','Mango','Watermelon');
 
//array_splice: Adding two elements to the $fruits array.
array_splice($fruits,0,0,['Avocado','Strawberries']);
 
 
//Output the array to console.
print_r($fruits);
 
/*Output
Array
(
    [0] => Avocado     
    [1] => Strawberries
    [2] => Apple       
    [3] => Orange      
    [4] => Mango       
    [5] => Watermelon  
)
*/
 
?>

3. Adding to the end of an array

Passing -1 to the start arguments sets the offset to the end of the array. However, the function adds elements all the way up from the second-last index position.

<?php
 
//Array with four elements.
$fruits = array('Apple','Orange','Mango','Watermelon');
 
//array_splice: Adding two elements to the $fruits array.
array_splice($fruits,-1,0,['Avocado','Strawberries']);
 
 
//Output the array to console.
print_r($fruits);
 
/*Output
Array
(
    [0] => Apple       
    [1] => Orange      
    [2] => Mango       
    [3] => Avocado     
    [4] => Strawberries
    [5] => Watermelon  
)
*/
 
?>

That’s all about the different methods to add to an array in PHP. We hope that you’ve enjoyed the article.

Want to explore further about PHP arrays?

We have many fun articles related to PHP arrays. You can explore these for learning more about arrays in PHP.

How to get the length of an array in PHP

How to print an array in PHP

Convert PHP array into a string with implode function


Article Categories

Article Tags

    © 2022 Confident.Systems