Specifications

There are a number of ways that we could have extracted numbers from these strings. Here we
used the function, intval(). As mentioned in Chapter 1, intval() converts a string to an
integer. The conversion is reasonably clever and will ignore parts, such as the label in this
example, that cannot be converted to an integer. We will cover various ways of processing
strings in the next chapter.
Other Array Manipulations
So far, we have only covered about half the array processing functions. Many others will be
useful from time to time.
Navigating Within an Array: each, current(), reset(), end(),
next(), pos(), and prev()
We mentioned previously that every array has an internal pointer that points to the current ele-
ment in the array. We indirectly used this pointer earlier when using the each() function, but
we can directly use and manipulate this pointer.
If we create a new array, the current pointer is initialized to point to the first element in the
array. Calling current( $array_name ) returns the first element.
Calling either next() or each() advances the pointer forward one element. Calling
each( $array_name ) returns the current element before advancing the pointer. The function
next() behaves slightly differentlycalling next( $array_name ) advances the pointer and
then returns the new current element.
We have already seen that reset() returns the pointer to the first element in the array.
Similarly, calling end( $array_name ) sends the pointer to the end of the array. The first and
last element in the array are returned by reset() and end(), respectively.
To move through an array in reverse order, we could use end() and prev(). The prev() func-
tion is the opposite of
next(). It moves the current pointer back one and then returns the new
current element.
For example, the following code displays an array in reverse order:
$value = end ($array);
while ($value)
{
echo “$value<br>”;
$value = prev($array);
}
If $array was declared like this:
$array = array(1, 2, 3);
Using PHP
P
ART I
88
05 7842 CH03 3/6/01 3:42 PM Page 88