Working with an Empty array in PHP

Working with an Empty array in PHP

Adding elements to an empty array is very important in that it helps to prevent errors when there is a faulty array. 

Here at Fixwebnode as part of our Website Support Services, we regularly help our Customers to fix PHP related errors.

In this context, we shall look into Working with empty array and adding elements to an Empty array in PHP.

 

Steps to add Elements to an Empty Array in PHP

1.Creating an Empty Array

To begin, we need to create an Empty array. 

Start by generating an empty PHP array with the array() function as shown below;

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

Once an Empty array is created, then elements needs to be added into it to help to save time in the process of debugging as well as in preventing errors as a result of a faulty array.

In PHP 5.4, the declaration "[]" is acceptable and used by most web developers. It can be declared as;

x $array = []

By using this, switching between JavaScript and PHP is possible and made flexible.

An instance of creating an empty array with "x $array = []" is stated below;

<?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( 3, 4);
          
foreach( $first as $value ) {
    echo "Value is $value <br>";
}
          
/* The Second method of creating an array. */
$first[0] = "three";
$first[1] = "four";
          
foreach( $first as $value ) {
    echo "Value is $value <br>";
}
?>

A shorter form of the above method is;
<?php 
  
// Create an empty array 
$emptyArray=array(); 
  
// Push elements to the array 
array_push($emptyArray, "water", "for", "water"); 
  
// Display array elements 
print_r($emptyArray); 
?>

2. Adding Elements to an Empty Array

After going through the process of creating an empty array, it is necessary to add elements to it. There are different ways to add elements to an array depending on your case.

i. Adding elements with "array_unshift" function

To add elements to the beginning of an array, the "array_unshift" function is recommended as shown below;

$ar = array('Women', 'Men', 'Cook', 'Biro');
// use array_unshift to add 2 elements to beginning of $ar
$num = array_unshift($ar, 'Goat', 'Money');
// inspect return value of array_unshift
echo $num; // 6
print_r($ar);
/* Array
(
    [0] => Goat
    [1] => Money
    [2] => Women
    [3] => Men
    [4] => Cook
    [5] => Biro
) */

 

ii. Adding elements with "array_push" function

You can also add elements to the end of your array with the xarray_push() function.

An example is shown below;

<?php
$a=array("red","black");
array_push($a,"yellow","green");
print_r($a);
?>

This function will add yellow and green colors to your array.

 

iii. Adding elements with "$cart [ ]" syntax

Another way of adding elements to an empty array is by using the "$cart [ ]" syntax.

An instance is given below;

$cart = array(); 
$cart[] = 15; 
$cart[] = 16;
for($i=0;$i<=5;$i++) { 
   $cart[] = $i; 

echo "<pre>"; print_r($cart); echo "</pre>";

 

Need support in creating an Array in PHP and other PHP related tasks? We are available to help you today.

 


Conclusion

Adding elements to an empty arrays in PHP. This article will take you through the steps you need to follow to add elements to an empty array.


icon

Sign up to our Newsletter

...and get one free support ticket.