Chapter 7. Using Arrays

I l @ ve RuBoard

The last type of variable I will discuss in this text is arrays. (Objects are beyond the scope this book, although once you are comfortable with PHP, you'll likely want to learn how to use them.)

Arrays constitute a complicated but very useful notion: they are a collection of multiple values assembled into one overriding variable. An array can consist of numbers and/or strings (and/or other arrays), which allows this one variable to hold exponentially more information than a simple string or number ever could. For example, if you wanted to create a grocery list using strings, your code would look something like:

 $Item1 = "apples"; $Item2 = "bananas"; $Item3 = "oranges"; 

For each added item, you would need to create a new string. This is cumbersome and it makes it difficult to refer back to the entire list or any specific value later in your code. You can greatly simplify matters by placing your entire list into one array (say, $Items), which contains everything you need to put on that list. As an array, your list can be augmented, sorted, searched, and so forth. With this context in mind, let's look into the syntax of arrays.

The other variable types you've dealt with ”numbers and strings ”have a variable name and a corresponding value (e.g., $FirstName which could be equal to "Larry"). Arrays also have a name , derived using the same conventions (the dollar sign followed by a letter or underscore , followed by any combination of letters , numbers, and the underscore), but arrays differ in that they contain what I will refer to as multiple elements. Each element has its own index or key (the two words can be used interchangeably), which is used to access that element's value. The terms index and key refer to a number or word which is used as a reference point to the values. An array can have either a number or a string as its key, depending upon how you set it up.

You may find it easiest to think of an array like a two-column table. The first column would be the index (acting like a row number or name) and the second column would be the value for that row. Using the index, you can determine the value stored at a particularly point in the table (Figures 7.1 and 7.2).

Figure 7.1. A spreadsheet table is a good metaphor for what an array is. Here the array which stores chapter titles uses numbers as its index (or key).

graphics/07fig01.gif

Figure 7.2. In this table version of the array, the index (or key) are words, not numbers.

graphics/07fig02.gif

Generally, the format for an array looks the same as that of any other variable, except that it will include a key in square brackets ([]) when referring to particular elements. So $Array refers to the array as a whole but $Array[0] points to the first element in the array. (Characters within a string are indexed beginning at zero, as explained in Chapter 5, Using Strings. The same is true when indexing the elements of an array.)

In this chapter, I'll discuss the fundamentals of handling arrays. I'll introduce a few of the key concepts and you'll learn how to incorporate them into your existing scripts.

I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net