Of course,
additional input checking would be necessary, but this should suffice to illustrate the
utility of array_walk().
?– Note If you??™re not familiar with PHP??™s form-handling capabilities, see Chapter 13.
Determining Array Size and Uniqueness
A few functions are available for determining the number of total and unique array
values. These functions are introduced in this section.
Determining the Size of an Array
The count() function returns the total number of values found in an array. Its prototype
follows:
integer count(array array [, int mode])
If the optional mode parameter is enabled (set to 1), the array will be counted recursively,
a feature useful when counting all elements of a multidimensional array. The
first example counts the total number of vegetables found in the $garden array:
$garden = array("cabbage", "peppers", "turnips", "carrots");
echo count($garden);
144 CHAPTER 5 ?– ARRAYS
This returns the following:
4
The next example counts both the scalar values and array values found in $locations:
$locations = array("Italy","Amsterdam",array("Boston","Des Moines"),"Miami");
echo count($locations,1);
This returns the following:
6
You may be scratching your head at this outcome because there appears to be
only five elements in the array. The array entity holding Boston and Des Moines is
counted as an item, just as its contents are.
?– Note The sizeof() function is an alias of count().
Pages:
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225