PHP next() 不工作

2024-01-21

我正在尝试使用 PHP 制作一个画廊。图像加载正确,但下一个和上一个按钮似乎不起作用。单击图片#1 下的“下一步”,您将看到图片#3,但是单击“图片#3”将您带到图片#2,这是正确的。我应该如何更改我的代码以使两者都按顺序进行?

    <?php
function listPicturesInDir($dir) {
    $dirname = "../pictures/photos/" . $dir . "/";
    $images = glob($dirname . "*.jpg");
    $previousPic = "null";
    foreach ($images as $image) {
        $next = next($images);
        $name = str_replace(".jpg", "", $image);
        $fp = strrpos($name, '/', 5) + 1;
        $name = substr($name, $fp, strlen($name));
        $id = str_replace(" ", "", $name);

        echo '<a href="#' . $id . '"><img class="galleryPics" src="' . $image . '" alt = "' . $name . '" title="'. $name.'"/></a>';
        echo '<div id="' . $id . '" class="modalDialog">';
        echo '<div>';
        if($previousPic !== "null"){
            echo'<a href="#'.$previousPic . '"><img src="../pictures/arrowLeft2.png" alt="Previous photograph" title= "Previous photograph" class="arrow"/></a> ';
        }

        if($next !== false){
            $name_next = str_replace(".jpg", "", $next);
            $fp_next = strrpos($name_next, '/', 5) + 1;
            $name_next2 = substr($name_next, $fp_next, strlen($name_next));
            $id_next = str_replace(" ", "", $name_next2);
            echo'<a href="#'.$id_next . '"><img src="../pictures/arrowRight2.png" alt="Next photograph" title="Next photograph" class="arrow"/></a>';
        }
        echo '<a href="#close" title="Close" class="close">X</a>';
        echo '<h2>' . $name . '</h2>';
        echo '<img class="modalImg" src="' . $image . '" alt = "' . $name . '"/>';
        echo '</div>';
        echo '';
        echo '</div>';
        //echo $next;
        $previousPic = $id;
    }
}
?>

问题是你正在使用next($images)在一个foreach ($images ...)语句,从而修改内部数组指针。这可能会导致意外的行为,正如指出的foreach 的文档 http://www.php.net/foreach:

As foreach依赖于内部数组指针,在循环内更改它可能会导致意外的行为。

这说明了你的问题,使用foreach and next:

$images = array('one', 'two', 'three', 'four');

foreach ($images as $image) {
    $next = next($images);
    echo "$image => $next", PHP_EOL;
}

Output:

one => three
two => four
three => 
four =>     

人们可能会认为只需更换next() with current()会有所帮助,但可惜的是:

foreach ($images as $image) {
    $next = current($images);
    echo "$image => $next", PHP_EOL;
}

Output:

one => two
two => two
three => two
four => two

根据评论foreach文档页面,该页面上曾经有一条通知,指出:

除非数组被引用,foreach对指定数组的副本而不是数组本身进行操作。foreach对数组指针有一些副作用。不要在 foreach 期间或之后依赖数组指针而不重置它。

不知道为什么它被删除,但如果我们使用参考$image然后它实际上起作用了(注意&):

foreach ($images as &$image) {
    $next = current($images);
    echo "$image => $next", PHP_EOL;
}

Output:

one => two
two => three
three => four
four => 

但也许老式的 for 循环更有意义:

for ($i = 0; $i < count($images); $i++) {
    $nextIndex = $i + 1;
    $next = ($nextIndex < count($images)) ? $images[$nextIndex] : null;
    $image = $images[$i];
    echo "$image => $next", PHP_EOL;
}

Output:

one => two
two => three
three => four
four => 

PHP 5.5.20 的输出。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP next() 不工作 的相关文章