Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. PHPBeginner - What are Associative Arrays?

What are Associative Arrays? - Images: Japanese

Hey there, everyone!

This is article 20 of the PHP beginner series. If you've been reading from the very start — nice work, seriously. Thanks for sticking with it this far. And if this is your first time here, welcome! Make yourself at home.

Next up, let's dig into something called an associative array. In other languages this concept goes by the name hash, map, or sometimes even object — but in PHP, associative array is the term you'll hear most often.

So what exactly is an associative array? A regular (indexed) array is a collection of data items identified by sequential numbers. An associative array, on the other hand, is a collection of data items each identified by a custom name.

Put simply: it's just an array where the index is a string instead of a number. Here's a regular indexed array to start:

<?php
$vocaloids = [];
$vocaloids[0] = '初音ミク';
$vocaloids[1] = 'IA';
$vocaloids[2] = '鏡音リン';

And here's the same idea written as an associative array:

<?php
$fruits = [];
$fruits['apple']  = 'Apple';
$fruits['orange'] = 'Orange';
$fruits['grape']  = 'Grape';

'apple' maps to 'Apple', 'orange' maps to 'Orange' — each string key is linked to a value. An associative array is essentially an array that uses strings (names) as its identifiers instead of numbers.

When would you use one? Whenever you want to manage data by name rather than by sequential number.

For example, if you want to store staff names alongside their contact details, you need to link each name to its corresponding number — which is a natural fit for an associative array rather than a plain indexed array.

Let's look at a concrete sample. We'll borrow the Vocaloid crew again. "Store each member's contact info in an associative array" looks like this:

<?php
$vocaloids = [];
$vocaloids['miku'] = '080-xxxx-1234';
$vocaloids['IA']   = '090-yyyy-1234';
$vocaloids['rin']  = '080-zzzz-1234';

To output 'miku's contact info from the above, you do this. Accessing an associative array works almost exactly the same as a regular array:

<?php
$vocaloids = [];
$vocaloids['miku'] = '080-xxxx-1234';
$vocaloids['IA']   = '090-yyyy-1234';
$vocaloids['rin']  = '080-zzzz-1234';

echo $vocaloids['miku']; // Outputs '080-xxxx-1234'.

Write the key string inside '[]' — that's all there is to it.

Each individual entry in an associative array is called a property. The value stored in a property is simply called the value. The name each property uses as its identifier is called a key, or property name.

<?php
$fruits['apple'] = 'Apple'; // Here, 'apple => Apple' is the property, 'apple' is the key, and 'Apple' is the value.

'Property', 'property name', and 'value' are terms you'll run into regularly in real-world work, so it's worth committing them to memory.

By the way, calling the property name (key) an "index" — just like with a regular array — is also fine, as is calling a property an "element". Different languages use different words for the same underlying concept.

Just like with indexed arrays, you can put variables and expressions inside '[]'. Here's an example where a variable is expanded and concatenated inside the brackets. It looks a bit wild, but this kind of thing is perfectly valid PHP:

<?php
$vocaloids = [];
$vocaloids['miku'] = '080-xxxx-1234';
$vocaloids['IA']   = '090-yyyy-1234';
$vocaloids['rin']  = '080-zzzz-1234';

$txt = 'mi';

echo $vocaloids[$txt . 'ku']; // Outputs '080-xxxx-1234'.

Still with me? Good. Now let's go over some important PHP-specific behavior around arrays and associative arrays. As mentioned in the previous article, PHP treats indexed arrays and associative arrays as the same underlying type — which means you can mix them together in a single array:

<?php
$vocaloids = [];
$vocaloids[]         = '初音ミク';
$vocaloids[1]        = 'IA';
$vocaloids['miku']   = '080-xxxx-1234';
$vocaloids['IA']     = '090-yyyy-1234';
$vocaloids['rin']    = '080-zzzz-1234';

var_dump($vocaloids);

Running this gives you:

array(5) {
  [0]=>
  string(12) "初音ミク"
  [1]=>
  string(2) "IA"
  ["miku"]=>
  string(13) "080-xxxx-1234"
  ["IA"]=>
  string(13) "090-yyyy-1234"
  ["rin"]=>
  string(13) "080-zzzz-1234"
}

No errors whatsoever. This means it's up to you as the programmer to keep track of whether a given array is being treated as indexed or associative. If you're coming from a language where the two types are strictly separate, this behavior might catch you off guard. Keep it in mind.

There's one more thing worth knowing: in PHP, arrays always preserve the order in which properties (elements) were added. Take a look at this example:

<?php
$vocaloids['miku'] = '080-xxxx-1234';
$vocaloids['IA']   = '090-yyyy-1234';
$vocaloids['rin']  = '080-zzzz-1234';

var_dump($vocaloids);

Running this gives you:

array(3) {
  ["miku"]=>
  string(13) "080-xxxx-1234"
  ["IA"]=>
  string(13) "090-yyyy-1234"
  ["rin"]=>
  string(13) "080-zzzz-1234"
}

The output comes out in exactly the order the entries were added. Guaranteed insertion order is a really useful property for associative arrays.

And this same insertion-order guarantee applies to indexed arrays too, since PHP treats them as the same type internally. That means the array is built in the order elements were added — not in the order of their index numbers.

Let's test that. Here's a sample where indexed array elements are deliberately added in a scrambled order:

<?php
$vocaloids[5] = '初音ミク';
$vocaloids[1] = 'IA';
$vocaloids[3] = '鏡音リン';

var_dump($vocaloids);

Running this gives you:

array(3) {
  [5]=>
  string(12) "初音ミク"
  [1]=>
  string(2) "IA"
  [3]=>
  string(12) "鏡音リン"
}

The output follows insertion order, not ascending index order. Guaranteed order is handy for associative arrays, but it can feel a little surprising when you see it with indexed arrays.

In practice, code that uses indexed arrays is often paired with the for loop we'll cover later, so this behavior rarely causes real problems. Still, it's good to know. Also worth noting: PHP won't complain if you skip index numbers entirely — but that can lead to subtle bugs if you're not careful.

In PHP, only strings and integers are valid as associative array keys.

Technically you can use values like true, false, or 3.14 as keys, but they get silently converted: true becomes 1, false becomes 0, and 3.14 gets truncated to 3. That kind of unexpected conversion is a recipe for bugs, so it's best to avoid those types as keys.

<?php
$test[0]     = 'hoge';
$test[false] = 'hoge1'; // 'false' is treated as '0', so this overwrites the entry above.

$test[1]    = 'fuga';
$test[1.21] = 'fuga1'; // '1.21' is truncated to '1', so this overwrites the entry above.

And that covers the basics of associative arrays. In the next article, we'll keep working with associative arrays and look at the double arrow operator. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .