What is an array in PHP and what types of arrays does PHP support?
Short answer
An array in PHP is a data structure that lets you store multiple values in a single variable, and PHP supports indexed arrays where elements are accessed by a numeric position, associative arrays where elements are accessed by a named key, and multidimensional arrays which are…
Advertisement
AModel answer
An array in PHP is a data structure that lets you store multiple values in a single variable, and PHP supports indexed arrays where elements are accessed by a numeric position, associative arrays where elements are accessed by a named key, and multidimensional arrays which are arrays containing other arrays, giving you a flexible way to organize related data together.
$fruits = ['apple', 'banana', 'cherry'];
$person = ['name' => 'Ali', 'age' => 30];Real-world example
An online store stores a customer's shopping cart items as an indexed array, while storing the customer's profile details, such as name and email, as an associative array for easy lookup by field name.
Likely follow-ups
- What is the difference between an indexed array and an associative array?
- How do you create a multidimensional array in PHP?
Related topics