Php new array
In PHP, you can create a new array in several ways:
-
Using the
array()
function:$array = array('key1' => 'value1', 'key2' => 'value2');
This creates a new array with the specified key-value pairs.
-
Using the
[]
syntax:$array = ['key1' => 'value1', 'key2' => 'value2'];
This is a more concise way to create an array, and is available in PHP 5.4 and later.
-
Using the
array()
constructor:$array = new ArrayObject(array('key1' => 'value1', 'key2' => 'value2'));
This creates a new
ArrayObject
instance, which is a more advanced type of array that supports additional features. -
Using the
[]
syntax with a single value:$array = ['value1', 'value2'];
This creates a new array with the specified values, and is often used when you need to create an array with a variable number of elements.
-
Using the
array_merge()
function:$array1 = array('key1' => 'value1'); $array2 = array('key2' => 'value2'); $array = array_merge($array1, $array2);
This creates a new array by merging two existing arrays.
Note that in PHP, arrays are a type of data structure that can store multiple values, and are often used to represent collections of data.