What is the difference between an indexed array, an associative array, and a multidimensional array in PHP?
Last updated
They're all the same underlying array type in PHP: an indexed array uses integer keys, an associative array uses named (string) keys, and a multidimensional array nests arrays as values.
Indexed array: Auto-numbered integer keys starting at
0:['a', 'b', 'c'].Associative array: Explicit string keys mapping to values:
['name' => 'Ann', 'age' => 30].Multidimensional array: Values are themselves arrays, forming nested tables or trees, accessed with chained keys like
$data[0]['name'].Key point: PHP arrays are ordered maps, so a single array can mix integer and string keys; the distinction is conceptual, not a separate type.
[lang:php]
$indexed = ['red', 'green'];
$assoc = ['r' => 255, 'g' => 128];
$matrix = [
['name' => 'Ann', 'age' => 30],
['name' => 'Bob', 'age' => 25],
];
echo $matrix[1]['name']; // Bob
- https://www.slideshare.net/slideshow/arrays-syntax-and-it-s-functions-in-php-pptx/273442508
- https://stackoverflow.com/questions/45683797/php-multidimensional-associative-arrays
- https://www.freecodecamp.org/news/how-to-use-arrays-in-php/
- https://stackoverflow.com/questions/3909285/i-am-so-confused-with-associative-arrays-in-php-two-and-3-dimensional
- https://www.php.net/manual/en/function.array-column.php
- https://stackoverflow.com/questions/45999272/how-to-convert-php-multi-dimensional-associative-array-to-simpler-indexed-array
- https://www.geeksforgeeks.org/php/difference-between-indexed-array-and-associative-array/
- https://www.geeksforgeeks.org/php/multidimensional-arrays-in-php/