How to make an array empty php
Learn how to make an empty array in PHP w/ example: create array, assign empty value, then unset values & keys.
Making an Empty Array in PHP
An empty array can be created in PHP in several ways. In this example, we will look at two different methods for creating an empty array. The first method is to use the$array = array();
syntax. This can be used to create an empty array and populate it with elements later.
The second method is to use the
syntax. This is a shorthand way of creating an empty array, and is the same as using $array = [];
. $array = array();
Once an empty array is created, you can add elements to it in a variety of ways. For example, you can use the
syntax to add an element to the end of an array. $array[] = $value;
You can also use the
syntax to add an element to the end of an array. array_push($array, $value);
You can also use the
syntax to add an element to the beginning of an array. array_unshift($array, $value);
Finally, you can use the
syntax to merge two arrays into one. array_merge($array1, $array2);
In summary, PHP provides several different methods for creating an empty array and populating it with elements. The two most common methods are using the
syntax, or the $array = array();
syntax. Once an empty array is created, you can add elements to it in a variety of ways, such as using the $array = [];
, $array[] = $value;
, or array_push($array, $value);
syntax. You can also use the array_unshift($array, $value);
syntax to merge two arrays into one. array_merge($array1, $array2);