Example 1 — List vs Associative Array
Check a normal array and an array with named keys.
<?php
$a = [1, 2, 3];
$b = ["a" => 1, "b" => 2];
echo array_is_list($a) ? "list" : "not a list", PHP_EOL;
echo array_is_list($b) ? "list" : "not a list", PHP_EOL; How It Works
- Create a numbered array. Its keys are automatically 0, 1 and 2.
- Create an array with names. Its keys are the strings
aandb. - Call array_is_list() on each. The first returns
true, the secondfalse. - Use a ternary. It prints a friendly word instead of 1 or an empty string.

