在这个 PHP foreach 循环中可能缺少一些非常简单的东西[已关闭]

问题描述 投票:0回答:1

这个问题是这个问题的延伸。

基本上,脚本比较两个数组并输出差异(向用户显示什么)。

$urlsToShow = array_diff($siteUrls, $seenUrls);
if (!empty($urlsToShow)) {
    // Echo the url to show for the iframe within browse.php and add an entry to the database that the user has seen this site
    foreach ($urlsToShow as $urlToShow) {
        echo $urlToShow;
        $entry = "INSERT INTO views VALUES ('', '$currentUsername', '$urlToShow')";
        mysqli_query($con, $entry);
        break;
    }
}

问题是我通过一次迭代在数据库中获得了两个条目?显示

$urlsToShow
数组中的第一个站点(回显到 iframe 中),并与同一数组中的下一个站点一起添加到数据库中。但用户永远不会看到下一个站点。

我是否错误地使用了

break

php sql arrays foreach break
1个回答
0
投票

使用

break;
可能不符合您的最佳利益。我建议使用
continue;

© www.soinside.com 2019 - 2024. All rights reserved.