Is your array associative or indexed?

← Is your array associative or indexed?

Ashterix

docs: English JsonRpcBundle docs, REST and MCP Bundle sections, package cards

en/articles/from-tg/php/php-arrays.md new file +70 βˆ’0

1 + # Is your array associative or indexed?
2 +
3 + In PHP, an array is a universal tool that doesn't require you to define its size, the type of its elements or even the structure of its keys. On the one hand that's convenient, but on the other β€” it's a problem.
4 +
5 + Most programming languages distinguish between arrays (indexed arrays) and dictionaries/maps (associative arrays), but in PHP it's one and the same structure: any array can become associative or numbered depending on the keys you use:
6 +
7 + ```php
8 + $a = [1, 2, 3, 4, 5]; // indexed
9 + $b = ['foo' => 6]; // associative
10 + $c = [
11 + ...$a,
12 + ...$b
13 + ]; // mixed
14 + ```
15 +
16 + You often need to handle arrays differently depending on their type. But PHP doesn't give you such a basic ability, because it doesn't distinguish between indexed and associative arrays at all, as the [documentation](https://www.php.net/manual/en/language.types.array.php) says explicitly.
17 +
18 + ## Will array_is_list help?
19 +
20 + You might think that `array_is_list` can help figure out which is which:
21 +
22 + ```php
23 + array_is_list($a); // true [0=>1, 1=>2, 2=>3, 3=>4, 4=>5]
24 + array_is_list($b); // false [foo=>6]
25 + array_is_list($c); // false [0=>1, 1=>2, 2=>3, 3=>4, 4=>5, foo=>6]
26 + ```
27 +
28 + At first glance everything is fine, but no.
29 +
30 + Let's extend our example: take only the odd numbers from the `$a` array.
31 +
32 + ```php
33 + $a2 = array_filter($a, fn($x) => $x % 2 !== 0); // 1,3,5
34 + array_is_list($a2); // false because 0=>1, 2=>3, 4=>5
35 + ```
36 +
37 + ### What happened to the array? Did it become associative?
38 +
39 + No. It's just that `array_filter` preserves the original keys. And although they are still numeric keys, because of the "holes" in the indexing `array_is_list` returns `false`.
40 +
41 + In other words, PHP doesn't put an equals sign between "numbered" and "indexed" arrays.
42 +
43 + ## String keys don't guarantee "associativity"
44 +
45 + One more unpleasant surprise that may be waiting for you.
46 +
47 + Many people believe that if an array has a string key, that's it, it's an associative array. But if the key looks like a number, PHP converts it to **int**. For example, product barcodes:
48 +
49 + ```php
50 + $products = [
51 + "0123456789052" => "Product",
52 + "8018075001428" => "Another product"
53 + ];
54 + print_r(array_keys($products));
55 + //Array
56 + //(
57 + // [0] => "0123456789052"
58 + // [1] => 8018075001428 // -- the string became a number
59 + //)
60 + ```
61 +
62 + So you can't rely on keys being "string or number" either.
63 +
64 + ## Conclusion
65 +
66 + > I've seen a lot of code where an array is used as a list, and then suddenly a string key is added β€” and that's it, data integrity is gone. If your logic really depends on what kind of array you have, check the actual key structure (via `array_keys`, `is_int`, `is_string` etc.) rather than `array_is_list`. And even better β€” don't be lazy: type and validate your input data, even if it's PHP.
67 +
68 + ---
69 +
70 + *First published in Ukrainian in the Telegram channel [β€œCreated to die”](https://t.me/php_while_true/57) on July 17, 2025.*