W3docs

How to Add Elements to an Empty Array in PHP

In this tutorial, we display and describe the most flexible ways to add elements to an empty array. Follow the steps and you will manage that easily.

This page provides a step-by-step guide on 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 <kbd class="highlighted">array()</kbd> language construct like this:

php create empty array

<?php

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

?>

While PHP automatically initializes arrays on first assignment, explicitly creating an empty array first can help prevent undefined variable warnings and make your code's intent clearer.

Please note that PHP 5.4+ supports the <kbd class="highlighted">[]</kbd> short array syntax. Most developers prefer <kbd class="highlighted">$array = []</kbd> because it is more concise and idiomatic.

Here is an example of creating an empty array with <kbd class="highlighted">$array = []</kbd>:

php create empty arrays

<?php

// Create an empty array
$firstempty = [];
echo "Created first empty array<br>";

// Create another empty array
$second = [];
echo "Created second empty array<br>";

?>

And, here is another shorter method:

php add elements to an empty array

<?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 proceed to adding elements to it. Below are several ways to add elements, depending on your exact needs.

Using array_unshift

If you intend to add elements to the beginning of the array, use the <kbd class="highlighted">array_unshift()</kbd> function like this:

php add element to empty array with array_unshift

<?php

$ar = [];

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

// inspect return value of array_unshift
echo $num; // 2
print_r($ar);
/* Array
(
    [0] => Lucy
    [1] => Marlo
) */

?>

Using array_push

If you intend to add elements to the end of your array, then you can use the <kbd class="highlighted">array_push()</kbd> function.

Here is an example:

php add elements to empty array with array_push

<?php

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

?>

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

Using the $array[] Syntax

The next way to add elements to an empty array is using the <kbd class="highlighted">$array[] =</kbd> syntax.

Here is an example:

add elements to empty array with $array[]

<?php

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

?>

This snippet demonstrates how 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.