How to Add Elements to an Empty Array in PHP

  1. 1. Create an Empty Array
  2. 2. Add Elements to an Empty Array
    1. A. Using array_unshift
    2. B. Using array_push
    3. C. Using $cart [ ]

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.

1. 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:

$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 x $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 x$array = []:

<?php
  
/* method of creating an Empty array. */
$firstempty = [];
echo "Created First empty array <br>";
      
/* method of creating the Second Empty array. */
$second = array( );
echo "Created second empty array<br>";
      
/* the First method of creating an array. */
$first = array( 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=array(); 
  
// Push elements to the array 
array_push($emptyArray, "geeks", "for", "geeks"); 
  
// Display array elements 
print_r($emptyArray); 
?>

2. 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.

A. 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:

$ar = array('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
) */

B. 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=array("white","blue");
array_push($a,"green","pink");
print_r($a);
?>

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

C. Using $cart [ ]

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

Here is an example:

$cart = array(); 
$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.


Do you find this helpful?

Related articles