PHP next()无法正常工作 [英] PHP next() not working

查看:47
本文介绍了PHP next()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP创建画廊.图像正确加载,但是下一个和上一个按钮似乎不起作用.在图片#1上单击下一步将带您进入图片#3,但在图片#3上单击返回将带您进入图片#2,这是正确的.我应该如何更改我的代码以使其都按顺序进行?

I'm trying to make a gallery using PHP. The images load properly, but the next and previous buttons don't seem to work. Clicking next on picture #1 brings you to picture #3 but clicking back on picture #3 bring you to picture #2 which is correct. How should I change my code to make it so both go in order?

    <?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;
    }
}
?>

推荐答案

问题是您正在 foreach($ images ...)<中使用 next($ images)/code>语句,从而修改内部数组指针.正如 foreach上的文档中指出的那样,这可能导致意外行为:

The problem is that you are using next($images) within a foreach ($images ...) statement, thus modifying the internal array pointer. This may lead to unexpected behavior, as pointed out in the documentation on foreach:

由于 foreach 依赖于内部数组指针,因此在循环中对其进行更改可能会导致意外行为.

As foreach relies on the internal array pointer, changing it within the loop may lead to unexpected behavior.

这使用 foreach next 来说明您的问题:

This illustrates your problem, using foreach and next:

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

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

输出:

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

有人可能会认为仅用 current()替换 next()会有所帮助,但可惜:

One may think that just replacing the next() with current() would help, but alas:

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

输出:

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

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

According to a comment on the foreach documentation page, there used to be a notice on said page stating that:

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

Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.

不知道为什么删除了它,但是如果我们使用 $ image 的引用,那么它实际上是有效的(请注意& ):

Don't know why that was removed, but if we use a reference for $image then it actually works (note the &):

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

输出:

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

但是也许循环的老派才更有意义:

But then maybe an old school for loop just makes more sense:

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;
}

输出:

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

PHP 5.5.20的输出.

Output from PHP 5.5.20.

这篇关于PHP next()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆