Ole Sørensen
Ole Sørensen

Reputation: 359

prev() and next() return no result

prev() and next() return no result, but current(), end() and reset() does as you can see here:

http://flamencopeko.net/songs_scans_skip_2.php
http://flamencopeko.net/songs_scans_skip_2.txt

<?php
   echo current($arrFiles);
?>
<br />prev: 
<?php
   echo prev($arrFiles);
?>
<br />next: 
<?php
   echo next($arrFiles);
?>
<br />end: 
<?php
   echo end($arrFiles);
?>
<br />reset: 
<?php
   echo reset($arrFiles);
?>

End goal is to make skip buttons change large scans. Some say it must be done in JS. I'm fine with both PHP and JS, but I completely fail to see how to write the needed functions.


This makes the array:

<?php
$arrFiles = array_diff(scandir("scans", 0), array(".", ".."));
$arrFiles = array_values($arrFiles);
$intCountFiles = count($arrFiles);
?>

Upvotes: 0

Views: 213

Answers (2)

srain
srain

Reputation: 9092

You call prev after you call current, the internal pointer in array will go out of the rang. It will not come back unless you call reset or end.

So after you have called current, the pointer point to index 0, then you called prev. The pointer went out of range, and returned false.

Then you called next, but the pointer was out of range, it could not move to next, so next also return false.

next acts like prev, once the pointer goes out of range, it will no come back, unless you call reset or end;

See the zend source code blow, it explains that:

    ZEND_API int zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos)
    {
        HashPosition *current = pos ? pos : &ht->pInternalPointer;

        IS_CONSISTENT(ht);

        if (*current) {
            *current = (*current)->pListLast;
            return SUCCESS;
        } else
            return FAILURE;
    }

Upvotes: 4

Manish Kumar
Manish Kumar

Reputation: 15497

please print your $arrayFiles array after the array_values() method and see what you are getting (proper array). These all methods work properly in PHP as expaling below

$people = array("Peter", "Joe", "Glenn", "Cleveland");

echo current($people) . "<br />";
echo next($people) . "<br />";
echo prev($people). "<br />";
echo end($people). "<br />";
echo reset($people). "<br />";

// result
Peter
Joe
Peter
Cleveland
Peter

Upvotes: 0

Related Questions