ANSWER:
The first thing that comes up when a conventional programmer thinks of an array (in PHP) would be something similar to:
$myArray1 = array(2016, "hello", 33);//option 1
$myArray2 = [2016, "hello", 33];//option 2
$myArray3 = [];//option 3
$myArray3[] = 2016;
$myArray3[] = "hello";
$myArray3[] = 33;
Where, if we wanted to call the array in some later part of the code, we could do:
echo $myArray1[1];// output: hello
echo $myArray2[1];// output: hello
echo $myArray3[1];// output: hello
The previous example showing the use of a Sequential Array
(since they increment the index used in a predetermined sequence, by incrementing by 1 for each following value).
So far so good. However, as humans, we might find it hard to remember that index [0]
of the array is the value of the year 2016, index [1]
of the array is a greetings, and index [2]
of the array is a simple integer value. The alternative we would then have is to use what is called an Associative Array. An Associative array has a few differences from a Sequential Array.
Differences (between a sequential and associative array):
Durring the declaration of an Associative Array, you don't only include the value
of what you want to put in the array, but you also put the index value (called the key
) which you want to use when calling the array in later parts of the code. The following syntax is used during it's declaration: "key" => "value"
.
When using the Associative Array, the key
value would then be placed inside the index of the array to retrieve the desired value
.
For instance:
$myArray1 = array(
"Year" => 2016,
"Greetings" => "hello",
"Integer_value" => 33);//option 1
$myArray2 = [
"Year" => 2016,
"Greetings" => "hello",
"Integer_value" => 33];//option 2
$myArray3 = [];//option 3
$myArray3["Year"] = 2016;
$myArray3["Greetings"] = "hello";
$myArray3["Integer_value"] = 33;
And now, to receive the same output as before, the key
value would be used in the arrays index:
echo $myArray1["Greetings"];// output: hello
echo $myArray2["Greetings"];// output: hello
echo $myArray3["Greetings"];// output: hello