Associative Arrays

Last updated

What is Associative Arrays?

Associative arrays in PHP are a type of array that uses named keys instead of numeric ones. This provides a more human-readable way to store data where each value can be accessed by its corresponding string key. An example of an associative array could be storing names as keys and their corresponding ages as values. Here's a brief example:

$ages = [
   "Peter" => 35,
   "John" => 42,
   "Mary" => 27
];

In this case, to find out John's age, you would simply use echo $ages['John'] where 'John' is the key. Associative arrays are also easy to loop through using the foreach construct.

Free resources to learn Associative Arrays

More in stage 3: Arrays

Associative Arrays: common questions

Can I learn Associative Arrays in Hindi?

This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Associative Arrays in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.

Is this Associative Arrays tutorial for beginners?

It is lesson 8 of 120 in the free PHP roadmap, in stage 3, Arrays. If you are new to PHP, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.

Still confused? Ask me