Consider the following array, which we will be using throughout this article:
$aFruit = array('Apple', 'Kiwi', 'Pear', 'Orange', 'Banana');
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.
0 1 2 3 4
next. By issuing the command like so:
The only argument these functions that move the internal pointer take are which array you're referring to. Our pointer for the
$aFruitarray now looks like this:
0 1 2 3 4
nextfunction 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
nextfunction there are other functions for moving the internal pointer. Namely:
Now it should seem fairly straightforward. The internal pointer is remembered for that particular session and so our 3 concurrent functions of
nextwill increment the pointer by 1 every time.
We've also introduced a new function, and that is current. The
currentfunction 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.
The good thing to remember is that internal pointers in PHP also work for associative arrays, and so an associative array like the following:
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.
We've already used the
currentfunction 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.
keyfunction is just like
currentbut it outputs the current key, and not the value itself.
As our array uses numerical keys, this will give us 0.
This returns a combination of the 2 above functions,
key. It is often used in conjunction with the
listconstruct 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:
Array (  => Apple [value] => Apple  => 0 [key] => 0 )
listconstruct, 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
whileloop to output them all if you wanted to. Although there are alternative methods as you've no doubt been using for many years now!
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:
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
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.