Resultingly,
if numerical keys are used, all corresponding values will be shifted down, whereas
arrays using associative keys will not be affected. Its prototype follows:
mixed array_shift(array array)
The following example removes the first state from the $states array:
$states = array("Ohio","New York","California","Texas");
$state = array_shift($states);
// $states = array("New York","California","Texas")
// $state = "Ohio"
136 CHAPTER 5 ?– ARRAYS
Removing a Value from the End of an Array
The array_pop() function removes and returns the last element from an array. Its
prototype follows:
mixed array_pop(array target_array)
The following example removes the last state from the $states array:
$states = array("Ohio","New York","California","Texas");
$state = array_pop($states);
// $states = array("Ohio", "New York", "California"
// $state = "Texas"
Locating Array Elements
The ability to efficiently sift through data is absolutely crucial in today??™s informationdriven
society. This section introduces several functions that enable you to search
arrays in order to locate items of interest.
Searching an Array
The in_array() function searches an array for a specific value, returning TRUE if the
value is found, and FALSE otherwise. Its prototype follows:
boolean in_array(mixed needle, array haystack [, boolean strict])
In the following example, a message is output if a specified state (Ohio) is found in
an array consisting of states having statewide smoking bans:
$state = "Ohio";
$states = array("California", "Hawaii", "Ohio", "New York");
if(in_array($state, $states)) echo "Not to worry, $state is smoke-free!";
The optional third parameter, strict, forces in_array() to also consider type.
Pages:
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218