Because I introduce so many functions in this chapter, I thought it wise to divide them into the following categories:
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: $states = array (0 => "Alabama", "1" => "Alaska"..."49" => "Wyoming"); Using numerical indexing, you could reference the first state like so: $states[0]
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: $states = array ("OH" => "Ohio", "PA" => "Pennsylvania", "NY" => "New York") You could then reference “Ohio” like so: $states["OH"] 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: $states = array ( You could then reference Ohio’s population like so: $states["Ohio"]["population"] This would return the following value: ==================================================== 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|