--- title: array --- ## Introduction to PHP Array An array can be thought of as a collection of items. ## Syntax An array is defined by array(), or []. An example of an array in each style can be seen below: ``` Value Arrays can also be defined with named keys, as shown below: ``` 'Suzuki', 'second favorite' => 'BMW', 'not my favorite' => 'Yamaha' ]; ``` ## Accessing Items Items within an array can be accessed by their corresponding key, or location within the array. For instance: ``` 'Suzuki', 'second favorite' => 'BMW', 'not my favorite' => 'Yamaha' ]; echo 'I like '. $bikes['not my favorite']; ``` Would produce the following output: ``` I like BWM ``` ## Multidimensional Array As we mentioned earlier arrays are collection of items, often times these items may be arrays of themselves. ![alt text](https://preview.ibb.co/hLBfcf/img.png "Screenshot of multidimensional arrays") You will always be able to get the value for the specific key by going down the layers: $arr['layerOne']['two'] ## Pitfalls When working with arrays, there are a few important things to keep in mind: 1) A comma after the last element is optional. 2) Named keys must be escaped to be accessed (i.e. $bikes[not my favorite] would not work). For more information, please see [PHP: Arrays](http://php.net/manual/en/language.types.array.php)