Specifying with array()

An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.

array( key => value , key2 => value2 , key3 => value3 , ... )

The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ) . For multi-line arrays on the other hand the trailing comma is commonly used, as it allows easier addition of new elements at the end.

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [] .

Example #1 A simple array <?php

$array = array(

"foo" => "bar" ,

"bar" => "foo" ,

);



// as of PHP 5.4

$array = [

"foo" => "bar" ,

"bar" => "foo" ,

];

?>

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

String integer + sign, will be cast to the integer "8" will actually be stored under 8 . On the other hand "08" will not be cast, as it isn't a valid decimal integer. s containing valid decimals, unless the number is preceded by asign, will be cast to thetype. E.g. the keywill actually be stored under. On the other handwill not be cast, as it isn't a valid decimal integer.

Float integer 8.7 will actually be stored under 8 . s are also cast tos, which means that the fractional part will be truncated. E.g. the keywill actually be stored under

Bool integer true will actually be stored under 1 and the key false under 0 . s are cast tos, too, i.e. the keywill actually be stored underand the keyunder

Null null will actually be stored under "" . will be cast to the empty string, i.e. the keywill actually be stored under

Array object Illegal offset type . s ands can not be used as keys. Doing so will result in a warning:

If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

Example #2 Type Casting and Overwriting example <?php

$array = array(

1 => "a" ,

"1" => "b" ,

1.5 => "c" ,

true => "d" ,

);

var_dump ( $array );

?> The above example will output: array(1) { [1]=> string(1) "d" } As all the keys in the above example are cast to 1 , the value will be overwritten on every new element and the last assigned value "d" is the only one left over.

PHP arrays can contain integer and string keys at the same time as PHP does not distinguish between indexed and associative arrays.

Example #3 Mixed integer and string keys <?php

$array = array(

"foo" => "bar" ,

"bar" => "foo" ,

100 => - 100 ,

- 100 => 100 ,

);

var_dump ( $array );

?> The above example will output: array(4) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "foo" [100]=> int(-100) [-100]=> int(100) }

The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.

Example #4 Indexed arrays without key <?php

$array = array( "foo" , "bar" , "hello" , "world" );

var_dump ( $array );

?> The above example will output: array(4) { [0]=> string(3) "foo" [1]=> string(3) "bar" [2]=> string(5) "hello" [3]=> string(5) "world" }

It is possible to specify the key only for some elements and leave it out for others: