How to Add Elements to an Empty Array in PHP

On this page, you have an opportunity to get a step-by-step guide, teaching how to add elements to an empty array in PHP.

Create an Empty Array

The first step should be creating an empty array.

You can generate an empty PHP array with the help of the array() function like this:

<?php

$emptyArray = []; 
$emptyArray = array();
$emptyArray = (array) null;

?>

It is essential to create an empty array and then push elements to it. The reason is that it assists in preventing errors linked to a faulty array. Also, it helps to save time in the process of debugging.

Please, take into account that PHP 5.4 supports [] as an alternative means. Hence, now most of the developers choose to work with $array = []. The primary reason is that it allows going back and forth between PHP and JavaScript easier.

Here is an example of creating an empty array with $array = []:

<?php

/* method of creating an Empty array. */
$firstempty = [];
echo "Created First empty array <br>";

/* method of creating the Second Empty array. */
$second = [];
echo "Created second empty array<br>";

/* the First method of creating an array. */
$first = [1, 2];

foreach ($first as $value) {
  echo "Value is $value <br>";
}

/* The Second method of creating an array. */
$first[0] = "one";
$first[1] = "two";

foreach ($first as $value) {
  echo "Value is $value <br>";
}

?>

And, here is another shorter method:

<?php

// Create an empty array
$emptyArray = [];

// Push elements to the array
array_push($emptyArray, "geeks", "for", "geeks");

// Display array elements
print_r($emptyArray);

?>

Add Elements to an Empty Array

After creating an empty array, you can get to adding elements to it. Below, you can find several ways to add elements to an array, depending on your exact needs.

Using array_unshift

If you intend to add elements to the beginning of the array, you are recommended to use the array_unshift() function like this:

<?php

$ar = ['Lili', 'Kiki', 'Lolo', 'Coco'];

// use array_unshift to add 2 elements to beginning of $ar
$num = array_unshift($ar, 'Lucy', 'Marlo');

// inspect return value of array_unshift
echo $num; // 6
print_r($ar);
/* Array
(
    [0] => Lucy
    [1] => Marlo
    [2] => Lili
    [3] => Kiki
    [4] => Lolo
    [5] => Coco
) */

?>

Using array_push

If you intend to add elements to the end of your array, then you can use the xarray_push() function.

Here is an example:

<?php

$a = ["white", "blue"];
array_push($a, "green", "pink");
print_r($a);

?>

This function will add green and pink colours to your array.

Using $cart [ ]

The next way to add elements to an empty array is using the $cart[]= syntax.

Here is an example:

<?php

$cart = [];
$cart[] = 13;
$cart[] = 14;
for ($i = 0; $i <= 5; $i++) {
  $cart[] = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";

?>

So, in this snippet we represented to you the steps to create an empty array and add elements to it. After reading and checking out the examples, you can choose the option that is more convenient for your project.