The array() function is used to create an array.
An Array is a structure which maps one value to another
ex: we can map the integer value 1 to the string "Monday"
The value 1 in this case is called a "key"
The string "Monday" is called the key's "value"
An array is made up of a group of these key/value pairs
An array is arranged so that each item in the array can be located by using the key when needed
Array Types are:
Associative (TIP: look for => it denotes the key value)
Numeric or Indexed (Integers keys are automatically or manually assigned, keys automatically start with 0, a common source of error)
Multidimensional (one or more values in the array is another array)
Note: A short array syntax exists which replaces array() with [ ].
Example #1 A simple array
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
$array = [
"foo" => "bar",
"bar" => "foo",
];
?>
The key can either be an int 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.
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 int and string keys at the same time as PHP does not distinguish between indexed and associative arrays.
Example #3 Mixed int 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 int key.
<?php
$array = array("foo", "bar", "hello", "world");
var_dump($array);
?>
The above example will output:
array(4) {
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:
Example #5 Keys not on all elements
<?php
$array = array(
"a",
"b",
6 => "c",
"d",
);
var_dump($array);
?>
The above example will output:
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[6]=>
string(1) "c"
[7]=>
string(1) "d"
}
As you can see the last value "d" was assigned the key 7. This is because the largest integer key before that was 6.
Example #6 Complex Type Casting and Overwriting example
This example includes all variations of type casting of keys and overwriting of elements.
<?php
$array = array(
1 => 'a',
'1' => 'b',
1.5 => 'c',
-1 => 'd',
'01' => 'e',
'1.5' => 'f',
true => 'g',
false => 'h',
'' => 'i',
null => 'j',
'k',
2 => 'l',
);
var_dump($array);
?>
The above example will output:
array(7) {
[1]=>
string(1) "g"
[-1]=>
string(1) "d"
["01"]=>
string(1) "e"
["1.5"]=>
string(1) "f"
[0]=>
string(1) "h"
[""]=>
string(1) "j"
[2]=>
string(1) "l"
}