duanpang2751 2019-02-27 18:25
浏览 226
已采纳

多个foreach循环彼此

I'm trying to do multiple foreach loops inside each other and stop at 12, this is what I have so far which isn't working. It shows exactly how I want it to. However, each file is supposed to loop through but it's showing image 1 over and over again for the first image in each directory.

<?php
date_default_timezone_set('Europe/London');
$dirname = "dir1";
$dirnameTwo = "dir2";
$dirnameThree = "dir3";

$cam1 = scandir($dirname, 1);
$cam2 = scandir($dirnameTwo, 1);
$cam3 = scandir($dirnameThree, 1);
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>

<head>
    <meta http-equiv='refresh' content='10'>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <meta http-equiv='cache-control' content='max-age=0' />
    <meta http-equiv='cache-control' content='no-cache' />
    <meta http-equiv='expires' content='0' />
    <meta http-equiv='expires' content='Tue, 01 Jan 1980 1:00:00 GMT' />
    <meta http-equiv='pragma' content='no-cache' />
</head>
<html>

<body>
<style type="text/css">
    .pi-title {
        padding: 1rem;
    }
</style>
<div class="container">
    <div class="row">
        <div class="pi-title">
            <h3>Test</h3>
        </div>
        <div class="table-container col-md-12">
            <table class="table" border='1' cellpadding='5' cellspacing='0' bordercolor='#ccc'>
                <thead class="thead-dark">
                <tr>
                    <th scope="col">ID</th>
                    <th scope="col">File Name</th>
                    <th scope="col">PI 1</th>
                    <th scope="col">PI 2</th>
                    <th scope="col">PI 3</th>
                </tr>
                </thead>
                <tbody>
                <tr></tr>
                <tr>
                    <?php
                    array_multisort(array_map('filectime', ($files = glob("*.*", GLOB_BRACE))), SORT_DESC, $files);
                    $dirs = array($dirname, $dirnameTwo, $dirnameThree);
                    $comma_separated = implode(",", $dirs);
                    $i = 1;
                    foreach ($cam1 as $cams1) {
                        foreach ($cam2 as $cams2) {
                            foreach ($cam3 as $cams3) {
                                foreach ($files as $filename) {
                                    if (file_exists($filename)) {
                                        echo "</tr>";
                                        echo "<td><font face='Arial' size='6'>$i</font></td>";
                                        echo "<td><font face='Arial' size='6' color='red'>" . date("F d Y H:i", filemtime($filename));
                                        echo "</font></td>";
                                    }
                                    print("

                                               <td><img src='$dirs[0]/$cams1' height='180' width='220'></td>
                                               <td><img src='$dirs[1]/$cams2' height='180' width='220'></td>
                                               <td><img src='$dirs[2]/$cams3' height='180' width='220'></td>

                           ");
                                    $i++;
                                    if ($i == 13) break;
                                }
                            }
                        }
                    }
                    ?>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

The loops keep on going past 12 and only shows the 1st file in each directory over and over again without getting each file in each directory.

  • 写回答

1条回答 默认 最新

  • dqx76962 2019-02-27 19:17
    关注

    I'm not entirely sure what you're trying to do - you haven't given any example input and output, and your code is written in a very unclear style - but there's a number of things that look wrong to me.


    Firstly, your innermost loop is not related to the other three in any way:

    foreach ($files as $filename)
    

    You define $files once, buried in the middle of this ugly line:

    array_multisort(array_map('filectime', ($files = glob("*.*", GLOB_BRACE))), SORT_DESC, $files);
    

    So the innermost loop is going to look at the same list of files over and over again.

    This also means that this line is redundant:

    if (file_exists($filename))
    

    Unless the file was deleted in the microseconds since PHP ran the glob command, this will always be true, because $files is populated with files that exist.


    Secondly, your nested loops will end up going through not just every item from each directory, but every possible combination. Consider the following miniature example:

    $cam1 = ['a', 'b'];
    $cam2 = ['c', 'd'];
    $cam3 = ['e', 'f'];
    $files = ['g', 'h'];
    foreach ($cam1 as $cams1) {
         foreach ($cam2 as $cams2) {
              foreach ($cam3 as $cams3) {
                  foreach ($files as $filename) {
                       echo "$cams1, $cams2, $cams3, $filename 
    ";
                  }
              }
         }
    }
    

    Outputs:

    a, c, e, g 
    a, c, e, h 
    a, c, f, g 
    a, c, f, h 
    a, d, e, g 
    a, d, e, h 
    a, d, f, g 
    a, d, f, h 
    b, c, e, g 
    b, c, e, h 
    b, c, f, g 
    b, c, f, h 
    b, d, e, g 
    b, d, e, h 
    b, d, f, g 
    b, d, f, h 
    

    It's hard to tell if that's what you intended.


    Thirdly, a break statement only breaks out of one loop, unless you give it an argument. So this line will break out of the foreach($files as $filename) loop:

    if ($i == 13) break;
    

    To break out of all the loops, you need to name how many to break out of:

    if ($i == 13) break 4;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化