【问题标题】:how to combine combine two foreach loops into one如何将两个foreach循环合并为一个
【发布时间】:2012-02-15 14:16:08
【问题描述】:

这是我到目前为止的代码,现在我想结合这个我能做什么?

<table>
            <tr>

                <th>Imported Files</th> 
                <th>Report Files</th> 
            </tr>
<?php

    $dir = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/u*.[cC][sS][vV]");
    $dir2 = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/r*.[cC][sS][vV]");



    foreach(glob($dir) as $file)  {
            echo "<tr>";
            echo "<td>". $file."</td>";
            }

            foreach(glob($dir2) as $file)  {
            echo "<td>". $file."</td>";
            }
            echo "</tr>";

?>
</table>

我想这样打印

            echo "<tr>";
    echo "<td>". $file1."</td>";
    echo "<td>". $file2."</td>";
    }
    echo "</tr>";

即在同一个 td 中我能做什么帮助我

update:-

我想把这个打印到 td

$dir = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/u*.[cC][sS][vV]");
$dir2 = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/r*.[cC][sS][vV]");

【问题讨论】:

  • 每个目录中的文件数量是否相同?您是说要在 dir2 中的第一个文件旁边显示 dir 中的第一个文件吗?

标签: php arrays


【解决方案1】:

这个处理不同大小的文件列表。

另外,您可以在同一个表格中显示多于两列的“文件列表”。

// retrieve data

$dirs = array(glob($dir), glob($dir2));

// display

$max_files = max(array_map('count', $dirs));
$count_dirs = count($dirs);

echo '<table>';

for($i = 0; $i < $max_files; $i++)
{
  echo '<tr>';
  for($d = 0; $d < $count_dirs; $d++)
  {
    $strFile = (isset($dirs[$d][$i]) ? $dirs[$d][$i] : '[NO FILE]');
    echo '<td>'.$strFile.'</td>';
  }
  echo '</tr>';
}

echo '</table>';

【讨论】:

  • 然后呢?我该怎么办?
  • :) 您的代码在任何测试页面中对我来说都可以正常工作,但是当我替换 str_replace 之后它实际上不能与 zend 一起工作,之后它也可以在 zend 中工作我能做些什么建议?????? ?????
  • 为什么不直接在处理前加上chdir("/var/www/13_Feb/subscriber-files");呢?然后你可以做$dir = 'u*.[cC][sS][vV]'; 等等,然后glob() 打电话......顺便说一句,如果你对我的回答感到满意,请不要忘记接受/赞成:)
【解决方案2】:
$line .= "<tr>";
foreach(glob($dir) as $file)  {     
    $line .= "<td>". $file."</td>";
}
foreach(glob($dir2) as $file)  {
    $line .= "<td>". $file."</td>";
}
$line .= "</tr>";

echo $line;

【讨论】:

    【解决方案3】:

    如果它们应该合并并且两者大小相同,也许这将是一个解决方案(未经测试)

    $dir1_files = glob($dir);
    $dir2_files = glob($dir2);
    
    for ($i =0; $i < count($dir1_files); $i++)
    {
        echo "<tr>";
        printf("<td>%s</td>", $dir1_files[$i]);
        printf("<td>%s</td>", $dir2_files[$i]);
        echo "</tr>";
    }
    

    【讨论】:

      【解决方案4】:

      PHP 5.3+ 对此有一个MultipleIterator,结合GlobIterator,您可以有这样的解决方案:

      $iterator = new MultipleIterator();
      $iterator->attachIterator(new GlobIterator($dir);
      $iterator->attachIterator(new GlobIterator($dir2);
      foreach ($iterator as $current) {
          echo "<tr>";
          echo "<td>". $current[0] ."</td>";
          echo "<td>". $current[1] ."</td>";
          echo "</tr>";
      }
      

      如果您有不同数量的文件并希望显示所有文件,可以使用MultipleIterator::MIT_NEED_ANY 标志:

      $iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
      $iterator->attachIterator(new GlobIterator($dir);
      $iterator->attachIterator(new GlobIterator($dir2);
      foreach ($iterator as $current) {
          echo "<tr>";
          echo "<td>". (isset($current[0]) ? $current[0] : '&nbsp;') ."</td>";
          echo "<td>". (isset($current[1]) ? $current[1] : '&nbsp;') ."</td>";
          echo "</tr>";
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-22
        • 1970-01-01
        • 2021-10-27
        • 2015-08-12
        相关资源
        最近更新 更多