What is an associative array?

An associative array is an example of an abstract data type. It comprises of (key, value) pairs, but any key can only appear once in our array.

Code

The following is an example of an associative array in PHP:

<?php
// Creating an array of a class of students
$students = array("Alex"=>"1A", "Mike"=>"2B", "Rachel"=>"3C");
// Accesing an individual element
echo "Rachel is in class " . $students['Rachel'];
echo "\n";
// Iterating through the associative array
foreach($students as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "\n";
}
?>

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved