A few functions are available for determining the number of total and unique array values. I’ll introduce these functions in this section. count() integer count(array input_array [, int mode]) The count() function, quite simply, returns the total number of values found in the input_array. 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. An example follows: $locations = array("Italy","Amsterdam",array("Boston","Des Moines"),"Miami"); This returns: =========================================================== 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.
array_count_values() array array_count_values(array input_array) The array_count_values() function returns an array consisting of associative key/value pairs. Each key represents a value found in the input_array, and its corresponding value denoting the frequency of that key’s appearance (as a value) in the input_array. An example follows: $states = array("Ohio","Iowa","Arizona","Iowa","Ohio"); $stateFrequency = array_count_values($states); This returns: ============================================================
Consider an example. Suppose you wanted to sort exam grades from lowest to highest: $grades = array(42,57,98,100,100,43,78,12); The outcome looks like this: =========================================================== It’s important to note that key/value associations are not maintained. Consider the following example: $states = array("OH" => "Ohio", "CA" => "California", "MD" => "Maryland"); Here’s the output: =========================================================== To maintain these associations, use asort(), introduced later in this section. natsort() void natsort(array target_array) The natsort() function is intended to offer a sorting mechanism comparable to those people normally use. The PHP manual offers an excellent example of what is meant by this; Consider the following items: picture1.jpg, picture2.jpg, picture10.jpg, picture20.jpg. Sorting these items using typical algorithms results in the following ordering: asort() void asort(array target_array [,integer sort_flags]) The asort() function is identical to sort(), sorting the target_array in ascending order, except that the key/value correspondence is maintained. Consider an array that contains the states in the order in which they joined the Union: $state[0] = "Delaware"; Sorting this array using sort() causes the associative correlation to be lost, which is probably a bad idea. Sorting using sort() and then outputting the results using print_r() results in the following: =========================================================== However, sorting with rsort() results in: ======================================================= If you use the optional sort_flags parameter, the exact sorting behavior is determined by its value, as described in the sort() section. array_multisort() boolean array_multisort(array array [, mixed arg [, mixed arg2...]]) The array_multisort() function can sort several arrays at once, and can sort multidimensional arrays in a number of fashions, returning TRUE on success and FALSE otherwise. It takes as input one or more arrays, each of which can be followed by flags that determine sorting behavior. There are two categories of sorting flags: order and type. Each flag is described in Table 5-1. Table 5-1. array_multisort() Flags
krsort() integer krsort(array array [,int sort_flags]) The krsort() function operates identically to ksort(), sorting by key, except that it sorts in reverse (descending) order. usort() void usort(array array, callback function_name) The usort() function offers a means for sorting an array using a user-defined comparison algorithm, embodied within a function. This is useful when you need to sort data in a fashion not offered by one of PHP’s built-in sorting functions. The user-defined function must take as input two arguments, and must return a negative integer, zero, or a positive integer based on whether the first argument is less than, equal to, or greater than the second argument. Not surprisingly, this function must be made available to the same scope in which usort() is being called. A particularly applicable example of usort() involves the ordering of American-format dates. Suppose that you want to sort an array of dates in ascending order: < ->php key that already exists in the resulting array, that key/value pair will overwrite the previously existing entry. This behavior does not hold true for numerical keys, in which case the key/value pair will be appended to the array. An example follows: $face = array("J","Q","K","A"); This returns something along the lines of the following (your results will vary because of the shuffle): Array ( [0] => 8 [1] => 6 [2] => K [3] => Q [4] => 9 [5] => 5 array_merge_recursive() array array_merge_recursive(array input_array1, array input_array2 [, array...]) The array_merge_recursive() function operates identically to array_merge(), joining two or more arrays together to form a single, unified array. The difference between the two functions lies in the way that this function behaves when a string key located in one of the input arrays already exists within the resulting array. array_merge() will simply overwrite the preexisting key/value pair, replacing it with the one found in the current input array. array_merge_recursive() will instead merge the values together, forming a new array with the preexisting key as its name. An example follows: $class1 = array("John" => 100, "James" => 85); This returns the following: =========================================================== Note that the key “John” now points to a numerically indexed array consisting of two scores. array_slice() array array_slice(array input_array, int offset [, int length]) The array_slice() function returns the section of input_array starting at the key offset, and ending at position offset + length. A positive offset value will cause the print_r($states); This returns: Array ( [0] => Alabama [1] => Alaska [2] => Arizona [3] => Arkansas ) Array ( [0] => California [1] => Connecticut ) You can use the optional parameter replacement to specify an array that will replace the target segment. An illustration follows: $states = array("Alabama", "Alaska", "Arizona", "Arkansas", "California", This returns the following: =========================================================== array_intersect() array array_intersect(array input_array1, array input_array2 [, array...]) The array_intersect() function returns a key-preserved array consisting only of those values present in input_array1 that are also present in each of other the input arrays. An example follows: $array1 = array("OH","CA","NY","HI","CT"); This returns: =========================================================== Note that array_intersect() only considers two items to be equal if they also share the same datatype. array_diff_assoc() array array_diff_assoc(array input_array1, array input_array2 [, array...]) The function array_diff_assoc() operates identically to array_diff(),except that it also considers array keys in the comparison. Therefore only key/value pairs located in input_array1, and not appearing in any of the other input arrays, will be returned in the result array. An example follows: $array1 = array("OH" => "Ohio", "CA" => "California", "HI" => "Hawaii"); This returns: ===========================================================
|
|
|
|
|
|
|
|