Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php

What is an associative array?

Sarvech Qadir

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";
}
?>

RELATED TAGS

php

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring