Arrays offer an ideal means for storing, manipulating, sorting, and retrieving the kinds of data sets that programmers spend a lot of time working with. PHP supports the array data type. This article explains the array-based features and functions it supports. It is excerpted from the book, Beginning PHP 5 and MySQL: From Novice to Professional, by W. Jason Gilmore (Apress, 2004; ISBN: 1893115518).
PROGRAMMERS SPEND A considerable amount of time working with sets of related data. The names of all employees at the XYZ corporation, U.S. Presidents and their corresponding birth dates, and years between 1900 and 1975 are all examples of related data sets. Because working with such groups of data is so prevalent, it might not come as a surprise that a means for managing these groups within code is a common feature across all mainstream programming languages. This means typically centers around the compound data type array, which offers an ideal means for storing, manipulating, sorting, and retrieving data sets. PHP’s solution is no different, supporting the array data type, in addition to an accompanying host of behaviors and functions directed towards array manipulation. In this chapter, you’ll learn all about those array-based features and functions supported by PHP. Although it is beyond the scope of this book to provide a comprehensive list of all predefined functions, you’ll nonetheless find information regarding the majority of array functions in this chapter, because PHP’s array-handling capabilities are just so darned useful.
Because I introduce so many functions in this chapter, I thought it wise to divide them into the following categories:
Testing for an array
Adding and removing array elements
Locating array elements
Determining array size and element uniqueness
Merging, slicing, splicing, and dissecting arrays
Rather than simply list them alphabetically, the categorical division should aid in later reference when searching for a viable solution to some future problem. Before beginning this review, I’ll start the chapter with a brief recap of the array definition and structure.
What Is an Array?
An array is traditionally defined as a group of items that share the same characteristics and are distinguished by their lookup key (more about this key in a bit). I use the word traditionally because you can flaunt this definition and group entirely unrelated entities together in an array structure. PHP takes this a step further, foregoing the requirement that they even share the same datatype. For example, an array might contain items like state names, zip codes, exam scores, or playing card suits. Each entity consists of two items: a key and a value. The key serves as the lookup facility for retrieving its counterpart, the value. These keys can be numerical, and bear no real relation to the value other than the value’s position in the array. As an example, the array could consist of an alphabetically sorted list of state names, with key 0 representing “Alabama”, and key 49 representing “Wyoming”. Using PHP syntax, this might look like this:
Using numerical indexing, you could reference the first state like so:
NOTE PHP’s numerically indexed arrays begin with position 0, not 1.
Alternatively, key mappings can be associative, where the key bears some relation to the value other than its array position. Mapping arrays associatively is particularly convenient when using numerical index values just doesn’t make sense. For example, I might want to create an array that maps state abbreviations to their names, like this: OH/Ohio, PA/Pennsylvania, and NY/New York. Using PHP syntax, this might look like:
Arrays consisting solely of atomic entities are referred to as being single-dimensional. Multidimensional arrays consist of other arrays. For example, you could use a multidimensional array to store U.S. state information. Using PHP syntax, it might look like this:
In addition to offering a means for creating and populating an array, the language must also offer a means for traversing it. As you’ll learn throughout this chapter, PHP offers many ways for doing so. Regardless of which way you use, keep in mind that all rely on the use of a central feature known as an array pointer. The array pointer acts like a bookmark, telling you the position of the array that you’re presently examining. You won’t work with the array pointer directly, but instead will traverse the array either using built-in language features or functions. Still, it’s useful to understand this basic concept.