How to set an Arrays internal pointer to a specific position? PHP/XML

Solution 1:

If your array is always indexed consistently (eg. 'page1' is always at index '0'), it's fairly simple:

$List = array('page1', 'page2', 'page3', 'page4', 'page5');
$CurrentPage = 3; // 'page4'

while (key($List) !== $CurrentPage) next($List); // Advance until there's a match

I personally don't rely on automatic indexing because there's always a chance that the automatic index might change. You should consider explicitly defining the keys:

$List = array(
    '1' => 'page1',
    '2' => 'page2',
    '3' => 'page3',
);

EDIT: If you want to test the values of the array (instead of the keys), use current():

while (current($List) !== $CurrentPage) next($List);

Solution 2:

Using the functions below, you can get the next and previous values of the array. If current value is not valid or it is the last (first - for prev) value in the array, then:

  • the function getNextVal(...) returns the first element value
  • the function getPrevVal(...) returns the last element value

The functions are cyclic.

function getNextVal(&$array, $curr_val)
{
    $next = 0;
    reset($array);

    do
    {
        $tmp_val = current($array);
        $res = next($array);
    } while ( ($tmp_val != $curr_val) && $res );

    if( $res )
    {
        $next = current($array);
    }

    return $next;
}

function getPrevVal(&$array, $curr_val)
{
    end($array);
    $prev = current($array);

    do
    {
        $tmp_val = current($array);
        $res = prev($array);
    } while ( ($tmp_val != $curr_val) && $res );

    if( $res )
    {
        $prev = current($array);
    }

    return $prev;
}

Solution 3:

Using the functions below, you can get the next and previous KEYs of the array. If current key is not valid or it is the last (first - for prev) key in the array, then:

  • the function getNext(...) returns 0 (the first element key)
  • the function getPrev(...) returns the key of the last array element

The functions are cyclic.

function getNext(&$array, $curr_key)
{
    $next = 0;
    reset($array);

    do
    {
        $tmp_key = key($array);
        $res = next($array);
    } while ( ($tmp_key != $curr_key) && $res );

    if( $res )
    {
        $next = key($array);
    }

    return $next;
}

function getPrev(&$array, $curr_key)
{
    end($array);
    $prev = key($array);

    do
    {
        $tmp_key = key($array);
        $res = prev($array);
    } while ( ($tmp_key != $curr_key) && $res );

    if( $res )
    {
        $prev = key($array);
    }

    return $prev;
}

Solution 4:

The internal array pointer is mainly used for looping over an array within one PHP script. I wouldn't recommend using it for moving from page to page.

For that, just keep track of the page number and the page size (number of items per page). Then, when you're loading another page, you can use them to decide which array items to show. For example:

$pageNum = $_GET["pageNum"];
$pageSize = 10;
$startIndex = ($pageNum - 1) * $pageSize;
$endIndex = ($startIndex + $pageSize) - 1;

(or something similar)

Solution 5:

another aproach without loops or search.

list($prev,$next) = getPrevNext($oObjects,$sCurrentKey);

function getPrevNext($aArray,$key){
    $aKeys = array_keys($aArray); //every element of aKeys is obviously unique
    $aIndices = array_flip($aKeys); //so array can be flipped without risk
    $i = $aIndices[$key]; //index of key in aKeys
    if ($i > 0) $prev = $aArray[$aKeys[$i-1]]; //use previous key in aArray
    if ($i < count($aKeys)-1) $next = $aArray[$aKeys[$i+1]]; //use next key in aArray
    return array($prev,$next);
}