php

How to Add Elements to an Empty Array in PHP?

In PHP, arrays are a fundamental data structure that enables you to organize and handle groups of data values. When working with arrays, you may need to add elements to an empty array. This operation can be accomplished using several methods in PHP.

In this article, we will explore different techniques to add elements to an empty array in PHP, along with examples and explanations.

Add Elements to an Empty Array in PHP

There are various methods in PHP for adding elements to an empty array:

1: Using array_push() Function

array_push() is a useful PHP function that helps you add one or more elements to the end of an array and with the help of this function, you can also add a single element or multiple elements to an empty array.

For example:

<?php
$myArray = array();
echo "Original Array: ";
print_r($myArray);
array_push($myArray, "Yellow", "Black");
echo "Modified Array: ";
print_r($myArray);
?>

 

The above code initializes an empty array $myArray, prints the original array using print_r() function, then uses the array_push() function to add the elements “Yellow” and “Black” to the array. Finally, it prints the modified array using print_r().

Output

2: Using array_unshift() Function

The array_unshift() function is another useful PHP function that you can use to add one or more elements to the array’s beginning and shift the rest of the elements to higher indexes. This function can also help you in adding elements to an empty array.

For example:

<?php
$myArray = array();
echo "Original Array: ";
print_r($myArray);
array_unshift($myArray, "Yellow", "Black");
echo "Modified Array: ";
print_r($myArray);
?>

 

The above code also initializes the empty array $myArray and uses the array_unshift() function to add the elements “Yellow” and “Black” to the array.

Output

3: Using $cart[]

In PHP, $cart[] is shorthand for adding an element to the end of an array. It’s equivalent to using the array_push() function with a single argument.

For example:

<?php
$cart = array();
echo "Original Array: ";
print_r($cart);
$cart[] = "Yellow";
$cart[] = "Black";
echo "Modified Array: ";
print_r($cart);
?>

 

The above code initializes an empty array called $cart and then uses the shorthand $cart[] syntax to add the elements “Yellow” and “Black” to the end of the array.

Output

Note: The cart is generally used term for the above method. However, you can name your array according to your choice.

Conclusion

The arrays are a vital data structure in PHP, allowing you to store and manage collections of values. Adding elements to an empty array is a common operation that can be done using various PHP methods such as array_push(), array_unshift(), and the shorthand $cart[] syntax. It is important to note that you can name your array as per your preference, although the term “cart” is commonly used for cart[] method.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.