DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

How to add elements to an array in PHP?

In this short tutorial, we look at how to add to array in PHP, we look at the different methods and use-cases and weigh them against each other.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts. However, in case you are here only for the solution use this link.

Table of Content

PHP: Add to array or append to array:

Appending an element into an array or add to 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 and they differ based on their use-cases.

In case you are looking to add to array, but want the element to be appended at the end of the array, you can look at the two methods. if you are looking to add to the beginning of the array the array_unshift does exactly that for you.

Add to array using square brackets:

The square bracket method to add to array is one of the most commonly used methods. Most articles on the topic recommend the use of this method because of its efficiency. In comparison to the other methods, it adds to array without the overhead of calling a function. But the downside is that it can only add one argument at a time. The syntax is as follows.

Syntax of Square Bracket method

$array[] = element
Enter fullscreen mode Exit fullscreen mode

Here array refers to the array you are adding to. And element is the element you are looking to add to array.

Code and Explanation

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

//Now, let's add to the array

$skillset[] = 'PHP';

//Output
var_dump($skillset);
Enter fullscreen mode Exit fullscreen mode

Output

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

As you can see, PHP was added to the end of the array. This method can be used to add to array in PHP. But although it is efficient, adding more than one element would be a hassle while using this method.

Using the array_push method:

The array_push is an inbuilt function in PHP that can be used to add to array as well. Where this method stands out from the previous method is that it can be used to add multiple elements to an array at once. The syntax is as follows.

Syntax of array_push

array_push($array , value1, value2, ..., value(n-1))
Enter fullscreen mode Exit fullscreen mode

Parameters

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

value1 - The value that you are looking to add to 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);
Enter fullscreen mode Exit fullscreen mode

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" }
Enter fullscreen mode Exit fullscreen mode

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 after you have used it to add to array in PHP. 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);
Enter fullscreen mode Exit fullscreen mode

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

Limitations and Caveats

  • Remember the square bracket is more efficient and should always be chosen when you are looking to add one or two elements to an array in PHP.
  • While using square brackets to add to array, 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.

Top comments (0)