Associative Array

இந்த வகை array-ல், ஒவ்வொரு element உடனும் ஒரு name அல்லது label-ஐ அடையாளமாய் பயன்படுத்தபடுகிறது. இதற்க்கு => என்ற symbol பயன்படுத்தபடுகிறது. இதனை கீழ்காணும் இரண்டு முறைகளில் define செய்யலாம்

Method -1

<?php
$state_capitals=array("Tamil Nadu"=>"Chennai","Karnataka"=>"Bangalore","Kerla"=>"Thiruvanandhapuram");
?>

Method -2

<?php
$state_capitals["Tamil Nadu"]="Chennai";
$state_capitals["Karnataka"]="Bangalore";
$state_capitals["Kerla"]="Thiruvanandhapuram";
?>

Associatie array real time example

<?php
$employee_salary= array("Priya"=>30000,"Sujitha"=>28000,"Chrishti"=>35000);

echo "Priya Salary is ".$employee_salary["Priya"]."<br>";
echo "Sujitha Salary is ".$employee_salary["Sujitha"]."<br>";
echo "Chrishti Salary is ".$employee_salary["Chrishti"]."<br>";
?>
Output
Priya Salary is 30000
Sujitha Salary is 28000
Chrishti Salary is 35000

Easy Way to traversing PHP associative array

<?php
$state_capitals=array("Tamil Nadu"=>"Chennai","Karnataka"=>"Bangalore","Kerla"=>"Thiruvanandhapuram");

foreach($state_capitals as $capital){
	echo $capital."<br>";
}
?>
Output
Chennai
Bangalore
Thiruvanandhapuram

Traversing PHP array with key and value

<?php
$state_capitals=array("Tamil Nadu"=>"Chennai","Karnataka"=>"Bangalore","Kerla"=>"Thiruvanandhapuram");

foreach($state_capitals as $key=> $capital){
	echo $key."=>".$capital."<br>";
}
?>
Output
Tamil Nadu=>Chennai
Karnataka=>Bangalore
Kerla=>Thiruvanandhapuram

Comments