PHP Tutorial
The associative arrays are very similar to numeric arrays in term of functionality.
Associative array will have their index as string so that you can establish a strong association between key and values.
There are two ways to create indexed arrays:
//index can be assigned automatically (index always starts at 0)
$variable = array("string1"=>"value1", "string2"=>"value2",.... "string3"=>"valueN");
//the index can be assigned manually
$variable["string1"] = "value1";
$variable["string2"] = "value2";
$variable["string3"] = "value3";
.
.
$variable["stringN"] = "valueN";
Let's put these syntax into real use.
Following is the example showing how to create and access associative arrays.
<?php
/* First method to associate create array. */
$age = array("sara" => 24, "kal" => 34, "will" => 15);
echo "age of sara is ". $age['sara'] . "<br />";
echo "age of kal is ". $age['kal']. "<br />";
echo "age of will is ". $age['will']. "<br />";
/* Second method to create array. */
$age['sara'] = 24;
$age['kal'] = 34;
$age['will'] = 15;
echo "age of sara is ". $age['sara'] . "<br />";
echo "age of kal is ". $age['kal']. "<br />";
echo "age of will is ". $age['will']. "<br />";
?>
This will produce the following result −
age of sara is 24
age of kal is 34
age of zara is 15
age of sara is 24
age of kal is 34
age of zara is 15