An array is a variable that can hold more than one value. Arrays in PHP are stored as value pairs that in other languages would be called a dictionary or a hashtable. Keys can be strings or integers.
There are several methods of declaring an array in PHP. The
array() function can be used, either with key-value pairs, or with values alone. Single brackets,
[...] can also be used in place of the
array() keyword. If any key value is omitted, the key will be found by incrementing the largest prior integer key. If a key is repeated, the new value will overwrite the prior key.
Additionally, when defining an array, the following key casts will occur:
- Strings containing valid
inttypes, unless preceded by a
+sign, will be cast to an
inttype key. As in the above example
"5"is treated as
floattypes will be cast to
inttypes, truncating the fractional part. As in the above example
5.7is treated as
booltypes are cast to
trueis stored as
nullwill be cast as the empty string,
- Arrays and objects cannot be used as keys and will result in an error:
Illegal offset type.