Arrays
If you have some experience with other programming languages or data structures in general, you might be aware of two data structures that are very common and useful: lists and maps. A list is an ordered set of elements, whereas a map is a set of elements identified by keys. Let's see an example:
List: ["Harry", "Ron", "Hermione"] Map: { "name": "James Potter", "status": "dead" }
The first element is a list of names that contains three values: Harry
, Ron
, and Hermione
. The second one is a map, and it defines two values: James Potter
and dead
. Each of these two values is identified with a key: name
and status
respectively.
In PHP, we do not have lists and maps; we have arrays. An array is a data structure that implements both, a list and a map.
Initializing arrays
You have different options for initializing an array. You can initialize an empty array, or you can initialize an array with data. There are different ways of writing the same data with arrays too. Let's see some examples...