An array in PHP is actually an ordered map.

$array = array(
    "foo" => "bar",
    "bar" => "foo",
    100   => -100,
    -100  => 100,
  • As array values can be other arrays, trees and multidimensional arrays are also possible.
  • The key can either be an integer or a string. The value can be of any type.
  • If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

Specifying with array()

An array can be created using the array() language construct.

The comma after the last array element is optional and can be omitted.

It takes any number of comma-separated key => value pairs as arguments.


    key  => value,
    key2 => value2,
    key3 => value3,

Short array syntax

$array = [
    "foo" => "bar",
    "bar" => "foo",

Replaces array() with [].

Key casts

$array = array(
    1    => "a",
    "1"  => "b",
    1.5  => "c",
    true => "d",
  • Strings containing valid decimal integers, unless the number is preceded by a + sign, will be cast to the integer type .
  • Floats are also cast to integers, which means that the fractional part will be truncated.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Arrays and objects can not be used as keys.

array — Structure map

Clickable & Draggable!

array — Related pages: