How to add elements to an array in PHP?

In this short tutorial, we look at how to add PHP elements to an array, the different methods to do so, and their use-cases.

However, in case you are here only for the solution use this link.

Table of Contents - PHP add to array

  • PHP: Add to array or append to array
  • Add to array using square brackets
  • Using the array_push method
  • PHP: Add to array - Limitations and Caveats
  • Other Related Concepts

PHP: Add to array or append to array:

Appending or adding an element into an array is a commonly used method not only in PHP but in other programming languages as well. However, adding elements to an array in PHP can be done with a handful of methods.

These methods vary based on their use cases. If you are looking to add an element at the end of the array, you can look at the two methods.

If you are looking to add elements to the beginning of the array, the array_unshift method can be used.

Add to array using square brackets:

The square bracket method to PHP add to array is one of the most commonly used methods.

Because of its efficiency, most programmers recommend the use of this method. In comparison to the other methods, it adds to an array without the overhead of calling a function.

But the downside is that it can only add one argument at a time.

Syntax of Square Bracket method

$array[] = element

Here array refers to the original array you are looking to append.

elementis the value you are looking to add to the array.

Code and Explanation

$skillset= array(
    'JavaScript',
    'Python',
    'C++'
);

//Now, let's add to the array
$skillset[] = 'PHP';

//Output
var_dump($skillset);

Output

array(4) { [0]=> string(10) "Javascript" [1]=> string(6) "Python" [2]=> string(3) "C++" [3]=> string(3) "PHP" }

As you can see, the above code can be used in PHP to add to array. But despite its efficiency, adding more than one element would be a hassle while using this method.

Using the array_push method:

The array_pushis another inbuilt function that can be used in PHP to add to arrays. This method can be used to add multiple elements to an array at once.

Syntax of array_push

array_push($array , value1, value2, ..., value(n-1))

Parameters:

array - Required, this parameter specifies the array you are looking to append

value1 - The value that you are looking to add to the array

Return Values

The array_push returns the number of elements in the array.

Code and Explanation:

$skillset= array(
    'JavaScript',
    'Python',
    'C++'
);

//Now, let's add to the array
array_push($skillset, 'PHP', 'HTML', 'CSS');

var_dump($skillset);

Output

array(6) { [0]=> string(10) "Javascript" [1]=> string(6) "Python" [2]=> string(3) "C++" [3]=> string(3) "PHP" [4]=> string(4) "HTML" [5]=> string(3) "CSS" }

As you can see the array_push in PHP adds to array the passed elements.

However, It is important to remember that the function returns the length and not the appended array. This is important because assigning the function to a variable and printing it would not return your desired output.

The below code explains the same.

$skillset= array(
    'JavaScript',
    'Python',
    'C++'
);

//Now, let's add to the array
$new_array = array_push($skillset, 'PHP', 'HTML', 'CSS');

echo($new_array);

This code outputs 6 which is the length of the updated array that you desired.

PHP: Add to array - Limitations and Caveats:

  • Remember that the square bracket is more efficient and should always be chosen when you are looking to add to an array.
  • While using the square bracket method, ensure that the name of the existing array is entered correctly because if the name passed is wrong a new array would be created.
  • The array_push returns a warning when the array you are looking to add to does not exist.