Page 140 - Beginning PHP 5.3
P. 140
Part II: Learning the Language
❑ Take a look at multidimensional arrays, which let you create rich, complex data structures
❑ Explore some of PHP ’ s powerful array - manipulation functions to do tricks such as sorting
arrays and merging arrays together
The Anatomy of an Array
Before diving into creating and using arrays, it ’ s worth taking a moment to explore the concept of an
array in more detail.
As already mentioned, an array is a single variable that can hold more than one value at once. You can
think of an array as a list of values. Each value within an array is called an element , and each element is
referenced by its own index , which is unique to that array. To access an element ’ s value — whether you ’ re
creating, reading, writing, or deleting the element — you use that element ’ s index. In this sense, arrays
share some similarity with strings, which you studied in the previous chapter. Just as you can access any
character of a string via its index, you can access any element of an array using the element ’ s index.
Many modern programming languages — including PHP — support two types of arrays:
❑ Indexed arrays — These are arrays where each element is referenced by a numeric index,
usually starting from zero. For example, the first element has an index of 0, the second has an
index of 1, and so on
❑ Associative arrays — This type of array is also referred to as a hash or map. With associative
arrays, each element is referenced by a string index. For example, you might create an array
element representing a customer ’ s age and give it an index of “ age ”
Although PHP lets you create and manipulate both indexed and associative arrays, all PHP arrays are in
fact of the same type behind the scenes. This can sometimes come in handy; for example, you can mix
numeric and string indices within the same array, or treat an indexed array like an associative array. In
practice, though, you generally want to work with one array type or another, and it helps to think of
indexed and associative arrays as different types of arrays.
An array index is often referred to as a key . Typically, a numeric index is called an index and a string
index is called a key; however there ’ s no hard - and - fast rule with this. You ’ ll see both terms used inter-
changeably in this book and elsewhere.
The actual values stored in array elements can be of any type, and you can mix types within a single
array. So, for example, an array might contain a string as its first element, a floating - point number as its
second element, and a Boolean value as its third element.
Creating Arrays
Powerful though they are, arrays in PHP are easy to create. The simplest way to create a new
array variable is to use PHP ’ s built - in array() construct. This takes a list of values and creates
an array containing those values, which you can then assign to a variable:
$authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );
102
9/21/09 9:00:08 AM
c06.indd 102
c06.indd 102 9/21/09 9:00:08 AM