Associative Arrays in PHP

Associative arrays are used to store key value pairs. For example, to store the marks of different subject of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subject’s names as the keys in our associative array, and the value would be their respective marks gained.

Example:
Here array() function is used to create associative array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php   
/* First method to create an associate array. */
$student_one = array("Maths"=>95, "Physics"=>90,  
                  "Chemistry"=>96, "English"=>93,  
                  "Computer"=>98); 
    
/* Second method to create an associate array. */
$student_two["Maths"] = 95; 
$student_two["Physics"] = 90; 
$student_two["Chemistry"] = 96; 
$student_two["English"] = 93; 
$student_two["Computer"] = 98; 
    
/* Accessing the elements directly */
echo "Marks for student one is:\n"
echo "Maths:" . $student_two["Maths"], "\n"
echo "Physics:" . $student_two["Physics"], "\n"
echo "Chemistry:" . $student_two["Chemistry"], "\n"
echo "English:" . $student_one["English"], "\n"
echo "Computer:" . $student_one["Computer"], "\n"
?> 

chevron_right


Output:

Marks for student one is:
Maths:95
Physics:90
Chemistry:96
English:93
Computer:98

Traversing the Associative Array:
We can traverse associative arrays using loops. We can loop through the associative array in two ways. First by using for loop and secondly by using foreach.

Example:
Here array_keys() function is used to find indices names given to them and count() function is used to count number of indices in associative arrays.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
    
/* Creating an associative array */
$student_one = array("Maths"=>95, "Physics"=>90,  
                  "Chemistry"=>96, "English"=>93,  
                  "Computer"=>98); 
    
    
/* Looping through an array using foreach */
echo "Looping using foreach: \n"
foreach ($student_one as $subject => $marks){ 
    echo "Student one got ".$marks." in ".$subject."\n"
   
/* Looping through an array using for */
echo "\nLooping using for: \n"
$subject = array_keys($student_one); 
$marks = count($student_one);  
    
for($i=0; $i < $marks; ++$i) { 
    echo $subject[$i] . ' ' . $student_one[$subject[$i]] . "\n"
?>  

chevron_right


Output:

Looping using foreach: 
Student one got 95 in Maths
Student one got 90 in Physics
Student one got 96 in Chemistry
Student one got 93 in English
Student one got 98 in Computer

Looping using for: 
Maths 95
Physics 90
Chemistry 96
English 93
Computer 98

Creating an associative array of mixed types

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php   
/* Creating an associative array of mixed types */
$arr["xyz"] = 95; 
$arr[100] = "abc"
$arr[11.25] = 100; 
$arr["abc"] = "pqr"
    
/* Looping through an array using foreach */
foreach ($arr as $key => $val){ 
    echo $key."==>".$val."\n"
}  
?> 

chevron_right


Output:

xyz==>95
100==>abc
11==>100
abc==>pqr


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.