An associative array stores each value under a key you choose, written as key => value: $user = ['name' => 'Ada', 'age' => 36];. Read a value with $user['name'], and add or change one by assigning to its key: $user['email'] = 'ada@example.com';.
In other languages the same structure is called a dictionary, a map or a hash. In PHP it is an ordinary array whose keys are strings instead of 0, 1, 2.
Add, change and remove pairs
Assigning to a key that does not exist adds it at the end. Assigning to a key that does exist replaces its value and keeps its position. unset() removes a pair.
array_push() does not help here: it can only append values with numeric keys. To add several pairs at once, use array_merge($stock, $more), where the new values win, or $stock + $more, where existing keys keep their values; the array_merge page compares the two.
Loop over keys and values
foreach ($array as $key => $value) walks the pairs in the order they were added. Leave out $key => when you only need the values.
Get all keys or all values
array_keys() returns the keys as a plain list and array_values() returns the values, both reindexed from 0. Give array_keys() a second argument and it returns only the keys holding that value, which is how you find every key for a value.
For one key, array_search() returns the first match, or false when there is none. The array_search page explains why you must compare that result with !== false.
Check if a key exists
isset($arr['key']) is the usual check and the fastest one, but it returns false when the key exists and holds null. array_key_exists('key', $arr) returns true in that case. For reading with a fallback, the ?? operator does both at once.
Sort by key or by value
ksort() sorts by key and asort() sorts by value, both keeping each key attached to its value. krsort() and arsort() sort in reverse. Do not use sort() on an associative array: it throws the keys away and renumbers from 0.
All of these sort in place and return true, so $sorted = ksort($arr) gives you true, not an array. Custom orders use uasort() and uksort().
Flip, count and convert to JSON
A few one-liners come up again and again with key-value data: array_flip() swaps keys and values, array_count_values() counts how often each value appears, and json_encode() turns the array into a JSON object.
Common mistake: reading a key that is not there
Reading a missing key returns null, but your own PHP also prints a warning:
<?php
$user = ['name' => 'Ada'];
echo $user['phone'];
Warning: Undefined array key "phone" in /var/www/html/index.php on line 3
Give the read a default with ??, which never warns, or check with isset() first when the missing case needs its own branch.
Inside a double-quoted string, wrap the lookup in braces, "{$user['email']}". Without them, PHP only accepts the unquoted form "$user[email]", which is easy to get wrong.
Frequently Asked Questions
How do I create an associative array in PHP?
Write each pair as key => value inside square brackets: $prices = ['apple' => 1.2, 'pear' => 0.9];. Read a value with $prices['apple'].
How do I sort an associative array by key in PHP?
Call ksort($arr); for A to Z or krsort($arr); for Z to A. Both sort in place, keep every value with its key, and return true, not the sorted array.
How do I loop through an associative array in PHP?
Use foreach with both variables: foreach ($prices as $fruit => $price) { echo "$fruit: $price\n"; }. The pairs come out in the order they were added.
How do I get all the keys of an associative array?
array_keys($arr) returns the keys as an indexed array, and array_values($arr) returns the values. array_keys($arr, $value) returns only the keys whose value equals $value.
Does a PHP associative array keep insertion order?
Yes. PHP arrays are ordered maps, so foreach, print_r and json_encode all see the pairs in the order you inserted them, until you sort the array with ksort(), asort() or a similar function.