PHP Basic Tutorial
You can create arrays in a variety of ways. Arrays are essential data structures for grouping multiple values in a single variable.
Using the array()
function:
Try it yourself
Using short array syntax ([ ]):
You can also use a shorter syntax by using the []
brackets:
Try it yourself
When creating indexed arrays the keys are given automatically to store values, starting at 0 and increased by 1 for each item, so the array above could also be created with keys:
Try it yourself
As you can see, indexed arrays are the same as associative arrays, but associative arrays uses custom keys (which can be strings or integers) to store values:
Try it yourself
You can also create an empty array and add elements to it later.
Try it yourself
The same goes for associative arrays, you can declare the array first, and then add items to it:
Try it yourself
Note
Array can have both indexed and named keys.