Often, the value of data can be determined by the ability to easily identify items of value, rather than the amount accumulated. If you arenít able to quickly navigate the information, little good is done accumulating it. In this section, Iíll introduce several functions that enable you to sift through arrays, in an effort to locate items of interest efficiently.
boolean in_array(mixed needle, array haystack [,boolean strict])
The in_array() function searches the haystack array for needle, returning TRUE if found, and FALSE otherwise. The optional third parameter, strict, forces in_array() to also consider type. An example follows:
$grades = array(100,94.7,67,89,100);
This string was output only once, because the second test required that the datatypes match. Because the second test compared an integer with a string, the test failed.
array array_keys(array target_array [, mixed search_value])
The array_keys() function returns an array consisting of all keys located in the array target_array. If the optional search_value parameter is included, only keys matching that value will be returned. An example follows:
$state["Delaware"] = "December 7, 1787";
boolean array_key_exists(mixed key, array target_array)
mixed key(array input_array)
The key() function returns the key element located at the current pointer position of input_array. Consider the following example:
$capitals = array("Ohio" => "Columbus", "Iowa" => "Des Moines","Arizona" => "Phoenix");
Note that key() does not advance the pointer with each call. Rather, you use the next() function, whose sole purpose is to accomplish this task. This function is formally introduced later in this section.
mixed reset(array input_array)
The reset() function serves to set the input_array pointer back to the beginning of the array. This function is commonly used when you need to review or manipulate an array multiple times within a script, or when sorting has completed.
array each(array input_array)
The each() function returns the current key/value pair from the input_array, and advances the pointer one position. The returned array consists of four keys, with keys 0 and key containing the key name, and keys 1 and value containing the corresponding data. If the pointer is residing at the end of the array before executing each(), FALSE will be returned.
boolean array_walk(array input_array, callback function [, mixed userdata])
The array_walk() function will pass each element of input_array to the user-defined function. This is useful when you need to perform a particular action based on each array element. Note that if you intend to actually modify the array key/value pairs, youíll need to pass each key/value to the function as a reference.
The user-defined function must take two parameters as input: the first represents the arrayís current value and the second the current key. If the optional userdata parameter is present in the call to array_walk(), then its value will be passed as a third parameter to the user-defined function.
You are probably scratching your head, wondering why this function could possibly be of any use (címon, you can admit it). Admittedly, I initially thought the same, until I did a bit of brainstorming. Perhaps one of the most effective examples involves the sanity-checking of user-supplied form data. Suppose the user was asked to provide six keywords that he thought best-described his state. That form source code might look like that shown in Listing 5-1.
Listing 5-1. Using an Array in a Form
blog comments powered by Disqus