Associative array is an second type of array.
You can define associative array in following two ways:
1 |
$variable_name = array(key1 => value1, key2 => value2, key3 => value3...key n => value n); |
OR
1 2 3 |
$variable_name["key1"] = "value1"; $variable_name["key2"] = "value2"; $variable_name["key3"] = "value3"; |
Example
1 2 3 4 5 6 7 8 |
<?php $Marks = array("Maths" => "60", "Science" => "45", "English" => "35"); echo "A student got marks in maths is ". $Marks["Maths"]."<br>"; echo "A student got marks in science is ". $Marks["Science"]."<br>"; echo "A student got marks in english is ". $Marks["English"]."<br>"; ?> |
Output:
1 2 3 |
A student got marks in maths is 60 A student got marks in science is 45 A student got marks in english is 35 |
1 2 3 4 5 6 7 8 |
<?php $Marks["Maths"] = "60"; $Marks["Science"] = "45"; $Marks["English"] = "35"; echo "A student got marks in maths is ". $Marks["Maths"]."<br>"; echo "A student got marks in science is ". $Marks["Science"]."<br>"; echo "A student got marks in english is ". $Marks["English"]."<br>"; ?> |
Output:
1 2 3 |
A student got marks in maths is 60 A student got marks in science is 45 A student got marks in english is 35 |