PHP allows you to associate name/label with each array elements in PHP using => symbol. Such way, you can easily remember the element because each element is represented by label than an incremented number.
There are two ways to define associative array:
1st way:
$salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
2nd way:
$salary["Sonoo"]="550000"; $salary["Vimal"]="250000"; $salary["Ratan"]="200000";
"550000","Vimal"=>"250000","Ratan"=>"200000"); echo "Sonoo salary: ".$salary["Sonoo"].""; echo "Vimal salary: ".$salary["Vimal"].""; echo "Ratan salary: ".$salary["Ratan"].""; ?>
Output:
Output:
By the help of PHP for each loop, we can easily traverse the elements of PHP associative array.
"550000","Vimal"=>"250000","Ratan"=>"200000"); foreach($salary as $k => $v) { echo "Key: ".$k." Value: ".$v.""; } ?>
Output: