Consider the following array, which we will be using throughout this article:
php Code:
By default the array pointer for any individual array will point to the first item. Each array has its very own internal pointer. Now consider the items in the array laid out like so, where the underline indicates where the internal pointer is by default.
Code:
0 1 2 3 4
next. By issuing the command like so:php Code:
The only argument these functions that move the internal pointer take are which array you're referring to. Our pointer for the
$aFruit array now looks like this:Code:
0 1 2 3 4
next function and echoing it will give us, well, let's see shall we! By now though you should already know.This would output Kiwi. In addition to the
next function there are other functions for moving the internal pointer. Namely:php Code:
Now it should seem fairly straightforward. The internal pointer is remembered for that particular session and so our 3 concurrent functions of
next will increment the pointer by 1 every time.We've also introduced a new function, and that is current. The
current function does not move the internal pointer anywhere but returns the item where the pointer is currently pointing to. Therefore the following would return Kiwi because we've moved the pointer forward one and then echoed out the current item - the one where the pointer now points to.php Code:
The good thing to remember is that internal pointers in PHP also work for associative arrays, and so an associative array like the following:
php Code:
Would still echo out the fruit Kiwi. Once you've mastered the internal pointers, and you should really have by now as there's not much to them, you can begin by using the functions that utilise the internal pointer. Let's go through them one-by-one.
Note: We'll be using the original array in all these examples below.
Current
We've already used the
current function previously. All this does is echo out the item where the pointer is currently pointing to.This will give us a very simple Apple in return.
Key
The
key function is just like current but it outputs the current key, and not the value itself.As our array uses numerical keys, this will give us 0.
Each
This returns a combination of the 2 above functions,
current and key. It is often used in conjunction with the list construct as I'll demonstrate in a moment. Once the function has been called, the pointer is advanced one.Would give us an array back which would look something like this:
Code:
Array
(
[1] => Apple
[value] => Apple
[0] => 0
[key] => 0
)
list construct, we can get both the key and the value for the particular item in the array to where the pointer currently points to.Which can also be used in a
while loop to output them all if you wanted to. Although there are alternative methods as you've no doubt been using for many years now!php Code:
For every time the each function, as aforementioned, the pointer is incremented. Therefore our loop would work like so, when talking about the array's pointer:
Code:
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
php Code:
That may seem fairly long-winded, and no doubt it is as there are simpler ways to achieve the same output, but it's always nice to have an alternative way to do things just in case. You never know when you may need the power and control that array pointers give you.


Join the friendly bunch on IRC...