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.
Here array() function is used to create associative array.
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.
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.
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
xyz==>95 100==>abc 11==>100 abc==>pqr
- How to loop through an associative array and get the key in PHP?
- How to remove a key and its value from an associative array in PHP ?
- Multidimensional Associative Array in PHP
- Convert an object to associative array in PHP
- How to get numeric index of associative array in PHP?
- How to check an array is associative or sequential in PHP?
- Iterate associative array using foreach loop in PHP
- PHP program to add item at the beginning of associative array
- How to access an associative array by integer index in PHP?
- PHP | Arrays
- Scala | Arrays
- Multidimensional arrays in PHP
- Sorting Arrays in PHP 5
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.