HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [216]
Making the most of multidimensional arrays
Using 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.
Using One-Dimensional Arrays
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.
Creating an array
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();
Now, $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 to explicitly define an array because it helps you remember the element is an array, and there are a few special cases (such as passing an array into a function) where the definition really matters.
Filling an array
An array is a container, so it’s a lot more fun if you put something in it. You can refer to an array element by adding an index (an integer) representing which element of the array you’re talking about.
Say I have the following array:
$spanish = array();
$spanish[1] = “uno”;
$spanish[2] = “dos”;
What I did here is to add two elements to the array. Essentially, I said that element 1 is uno, and element 2 is dos.
PHP has another interesting trick available. Take a look at the next line:
$spanish[] = “tres”;
This seems a little odd because I didn’t specify an index. PHP is pretty helpful. If you don’t specify an index, it looks at the largest index already used in the array and places the new value at the next spot. So, the value tres will be placed in element 3 of the array.
PHP is somewhat notorious for its array mechanism. Depending on how you look at it, PHP is far more forgiving or far sloppier than most languages when it comes to arrays. For example, you don’t have to specify the length of an array. PHP just makes the array whatever size seems to work. In fact, you don’t even have to explicitly create the array. When you start using an array, PHP automatically just makes it if it isn’t already there. Although this is pretty easy, I’ve seen enough science fiction movies to know what can happen when we let computers make all the decisions for us.
Viewing the elements of an array
You can access the elements of an array in exactly the same way you created them. Array elements are just variables; the only difference is the numeric index. Here’s one way to print out the elements of the array:
print <<< HERE
One: $spanish[1]
Two: $spanish[2]
Three: $spanish[3]
HERE;
I can simply print out the array elements like any ordinary variable. Just remember to add the index.
Another great way to print out arrays is particularly useful for debugging. Take a look at this variation:
print “
\n”;\n”;print_r($spanish);
print “
The print_r() function is a special debugging function. It allows you to pass an entire array, and it prints out the array in an easy-to-read format. It’s best to put the output of the print_r() function inside a
element so that the output is preserved.Of course, the results of the print_r() function mean something to you, but your users don’t care about arrays. This is only