This chapter introduces arrays and the language??™s impressive variety of functions
used to work with them. Specifically you??™ll learn how to do the following:
??? Create arrays
??? Output arrays
??? Test for an array
??? Add and remove array elements
??? Locate array elements
??? Traverse arrays
??? Determine array size and element uniqueness
??? Sort arrays
??? Merge, slice, splice, and dissect arrays
128 CHAPTER 5 ?– ARRAYS
Before beginning the overview of these functions, let??™s take a moment to formally
define an array and review some fundamental concepts on how PHP regards this
important datatype.
What Is an Array?
An array is traditionally defined as a group of items that share certain characteristics,
such as similarity (car models, baseball teams, types of fruit, etc.) and type (e.g., all
strings or integers). Each item is distinguished by a special identifier known as a key.
PHP takes this definition a step further, forgoing the requirement that the items share
the same datatype. For example, an array could quite possibly contain items such as
state names, ZIP codes, exam scores, or playing card suits.
Each item consists of two components: the aforementioned key and a value. The
key serves as the lookup facility for retrieving its counterpart, the value. Keys can be
numerical or associative. Numerical keys bear no real relation to the value other than
the value??™s position in the array.
Pages:
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209