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

$array = [];
syntax. This is a shorthand way of creating an empty array, and is the same as using
$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

$array[] = $value;
syntax to add an element to the end of an array.

You can also use the

array_push($array, $value);
syntax to add an element to the end of an array.

You can also use the

array_unshift($array, $value);
syntax to add an element to the beginning of an array.

Finally, you can use the

array_merge($array1, $array2);
syntax to merge two arrays into one.

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

$array = array();
syntax, or the
$array = [];
syntax. Once an empty array is created, you can add elements to it in a variety of ways, such as using the
$array[] = $value;
,
array_push($array, $value);
, or
array_unshift($array, $value);
syntax. You can also use the
array_merge($array1, $array2);
syntax to merge two arrays into one.

Answers (0)