PHP Associative Arrays
PHP associate array is used to access the elements with keys by “=>” symbol. There are two ways to create an associative array. 1st way:
$age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29");2nd way: $age['john'] = "20"; $age['Helena'] = "30"; $age['Rasmus'] = "29"; Let us take an example: Example1:
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); echo "Helena is " . $age['Helena'] . " years old."; ?> </body> </html>Example2
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); // Traversing PHP Associative Array foreach($age as $k => $v) { echo "Key: ".$k." Value: ".$v."<br/>"; } ?> </body> </html>Output Key: John Value: 20 Key: Helena Value: 30 Key: Rasmus Value: 29