PHP Associative Array.

Techno PHP

The Associative Array is one of the powerful and most efficient arrays in PHP.In associative array, we can associate name with each array elements in PHP using => symbol.
We define the key for the value. At the time of execution, we pass the key and It gives the related value as output .
A simple example of associative array .


Example

<?php 
$number=array("vishal"=>67,"Manish"=>32,"Pankaj"=>32,"Hemant"=>89)
?>
Marks : <?php echo"$number[vishal]"; 
?>
Marks :<?php echo "$number[Manish]"; 
?>
Marks :<?php echo "$number[Pankaj]"; 
?>
Marks :<?php echo "$number[Hemant]"; 
?>
Run
"vishal"=>67

"Manish"=>32

"Pankaj"=>32

"Hemant"=>89

"Key"=>Value

In above example we have four students names are Vishal ,Manish ,Pankaj ,Hemant .
We have to find out the Marks of one by one student but we do not know about Vishal, Manish, Pankaj, Hemant roll numbers so we create the keys like Vishal, Manish, Pankaj, Hemant.
…Means - Vishal is a key, Manish is also a key, Pankaj is also a key, Hemant is also key and associates with values .
These key related to marks (value).

After that we find the marks by the name (key) we do not need any roll number for find out the marks .

We pass the name of student only and after execution, we got associate value(marks) of students.

This is the major advantage of the associative array. Key=>value . Key associates with value. .

Related