In This Chapter
Creating one-dimensional arrays
Making the most of multidimensional arrays
foreach loops to simplify array management
Breaking a string into an array
In time, arrays will become one of the most important tools in your toolbox. They can be a bit hard to grasp for beginners, but don't let that stop you. Arrays are awesome because they allow you to quickly apply the same instructions to a large number of items.
In PHP, an array is a variable that holds multiple values that are mapped to keys. Think of a golfing scorecard. You have several scores, one for each hole on the golf course. The hole number is the key, and the score for that hole is the value. Keys are usually numeric, but values can be any type. You can have an array of strings, numbers, or even objects. Any time you're thinking about a list of things, an array is the natural way to represent this list.
The most basic array is a one-dimensional array, which is basically just one container with slots. Each slot has only one variable in it. In this section, you find out how to create this type of array and fill it.
Array creation is pretty simple. First, you need to create a variable and then tell PHP that you want that variable to be an array:
$theVar = array();
$theVar is an array. However, it's an empty array waiting for you to come along and fill it.
Technically, you can skip the variable creation step. It's still a good idea ...