TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Advertisement
Using the Internal Array Pointers to Access Elements in Arrays
   The native array pointer functions in PHP allow you to traverse an array without actually referring to specific array elements. They can be particularly useful if you're using an associative array and wish to use the first element in the array. An internal array pointer, in PHP, is fundamentally an invisible pointer that points to a particular record in an array.

Consider the following array, which we will be using throughout this article:

php Code:
$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.

Code:
0 1 2 3 4
To move the pointer to the next item in the array, we can use a function called next. By issuing the command like so:

php Code:
next($aFruit);

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
This may seem all fine and dandy, but perhaps at the moment it's a little perplexing and thus seemingly pointless. Bear with us! We can echo out any of these array pointer functions and it will echo the item in the array that the pointer is at after the function has been executed. Thus issuing the next function and echoing it will give us, well, let's see shall we! By now though you should already know.

php Code:
$aFruit = array('Apple', 'Kiwi', 'Pear', 'Orange', 'Banana');
echo next($aFruit);

This would output Kiwi. In addition to the next function there are other functions for moving the internal pointer. Namely:

php Code:
$aFruit = array('Apple', 'Kiwi', 'Pear', 'Orange', 'Banana');

echo next($aFruit); // Kiwi
echo prev($aFruit); // Apple
echo end($aFruit); // Banana
echo reset($aFruit); // Apple

next($aFruit);
next($aFruit);
next($aFruit);

echo current($aFruit); // Orange
 

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:
$aFruit = array('Apple', 'Kiwi', 'Pear', 'Orange', 'Banana');

next($aFruit);
echo current($aFruit);

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:
$aFruit = array('Fruit 1' => 'Apple', 'Fruit 2' => 'Kiwi');

next($aFruit);
echo current($aFruit);

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.

php Code:
echo current($aFruit);

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.

php Code:
echo key($aFruit);

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.

php Code:
print_r(each($aFruit));

Would give us an array back which would look something like this:

Code:
Array
(
    [1] => Apple
    [value] => Apple
    [0] => 0
    [key] => 0
)
Used in conjunction with the 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.

php Code:
list($szKey, $szValue) = each($aFruit);

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:
while(list($szKey, $szValue) = each($aFruit))
{
    echo 'Key: ' . $szKey . ' and value: ' . $szValue . '<br />';
}

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
Array pointers should be quite clear as to how you use them. You may also use the array pointer functions in conjunction with multidimensional arrays, especially multidimensional arrays that are also associative.

php Code:
$aFruit = array (
                    'Tried' => array('Apple', 'Kiwi'),
                    'Not Tried' => array('Pear', 'Orange', 'Banana')
                );

while($aChild = key($aFruit))
{
    while($szItem = current($aFruit[$aChild]))
    {
        echo current($aFruit[$aChild]) . '<br />';
        next($aFruit[$aChild]);
    }
   
    next($aFruit);
}

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.
Report this Article
Last 5 Article Reviews Read All Reviews
   arrays made ez
Review added by Unregistered on 01-19-2008
thanks arrays seem easier to understand now.

thank you for that.

All times are GMT. The time now is 07:06 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0