Associative Array

An associative array is where we assign unique IDs (key) to our values. By default the keys are will be numbers as shown in Numeric Array tutorial. We can set these keys to something else to make it easier to definate the values.

Here’s an example as how to define an associative array:
[php]
$seasons = array("Summer"=>"Orange", "Autumn"=>"Brown", "Winter"=>"Blue");
[/php]

We can also define it like this:
[php]
$seasons["Summer"] = "Orange";
$seasons["Autumn"] = "Brown";
$seasons["Winter"] = "Blue";
[/php]

The output will be the same

Array = (
[“Summer”] = “Orange”,
[“Autumn”] = “Brown”,
[“Winter”] = “Blue”
)

Adding to an associative array
You can add another element to your array by declaring another key in your array, like so:
[php]
$seasons["Spring"] = "Green";
[/php]

The output will now be:

Array = (
[“Summer”] = “Orange”,
[“Autumn”] = “Brown”,
[“Winter”] = “Blue”,
[“Spring”] = “Green”
)