Specifications

Using PHP
P
ART I
70
This chapter shows you how to use an important programming constructarrays. The vari-
ables that we looked at in the previous chapters are scalar variables, which store a single value.
An array is a variable that stores a set or sequence of values. One array can have many ele-
ments. Each element can hold a single value, such as text or numbers, or another array. An
array containing other arrays is known as a multidimensional array.
PHP supports both numerically indexed and associative arrays. You will probably be familiar
with numerically indexed arrays if youve used a programming language, but unless you use
PHP or Perl, you might not have seen associative arrays before. Associative arrays let you use
more useful values as the index. Rather than each element having a numeric index, they can
have words or other meaningful information.
We will continue developing the Bobs Auto parts example using arrays to work more easily
with repetitive information such as customer orders. Likewise, we will write shorter, tidier
code to do some of the things we did with files in the previous chapter.
Key topics covered in this chapter include
What is an array?
Numerically indexed arrays
Associative arrays
Multidimensional arrays
Sorting arrays
Further reading
What Is an Array?
We looked at scalar variables in Chapter 1, PHP Crash Course. A scalar variable is a named
location in which to store a value; similarly, an array is a named place to store a set of values,
thereby allowing you to group common scalars.
Bobs product list will be the array for our example. In Figure 3.1, you can see a list of three
products stored in an array format and one variable, called
$products, which stores the three
values. (Well look at how to create a variable like this in a minute.)
Tires Oil
product
Spark Plugs
F
IGURE 3.1
Bobs products can be stored in an array.
05 7842 CH03 3/6/01 3:41 PM Page 70