Searching Associative Array Keys
The function array_key_exists() returns TRUE if a specified key is found in an array,
and returns FALSE otherwise. Its prototype follows:
boolean array_key_exists(mixed key, array array)
CHAPTER 5 ?– ARRAYS 137
The following example will search an array??™s keys for Ohio, and if found, will output
information about its entrance into the Union:
$state["Delaware"] = "December 7, 1787";
$state["Pennsylvania"] = "December 12, 1787";
$state["Ohio"] = "March 1, 1803";
if (array_key_exists("Ohio", $state))
printf("Ohio joined the Union on %s", $state["Ohio"]);
The following is the result:
Ohio joined the Union on March 1, 1803
Searching Associative Array Values
The array_search() function searches an array for a specified value, returning its key
if located, and FALSE otherwise. Its prototype follows:
mixed array_search(mixed needle, array haystack [, boolean strict])
The following example searches $state for a particular date (December 7), returning
information about the corresponding state if located:
$state["Ohio"] = "March 1";
$state["Delaware"] = "December 7";
$state["Pennsylvania"] = "December 12";
$founded = array_search("December 7", $state);
if ($founded) printf("%s was founded on %s.", $founded, $state[$founded]);
The output follows:
Delaware was founded on December 7.
Retrieving Array Keys
The array_keys() function returns an array consisting of all keys located in an array.
Pages:
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219