2012-02-15 129 views
1

这是我的代码到目前为止,现在我想结合这个我能做什么?如何结合两个foreach循环合并为一个

<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]"); 
+0

每个目录中的文件数是否相同?你是说你想在dir2中的第一个文件旁边的dir中显示第一个文件吗? – 2012-02-15 14:19:47

回答

0

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

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

// 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>'; 
+0

:)这里是我的编码http://pastebin.com/DQdiBarL – John 2012-02-15 14:39:51

+0

而且?我应该怎么做? – 2012-02-15 15:07:39

+0

:)你的代码在任何测试页面都可以正常工作,但实际上并没有与zend一起工作,当我替换str_replace之后,它也在zend中工作,我可以做任何建议??????????? – John 2012-02-15 15:16:01

0
$line .= "<tr>"; 
foreach(glob($dir) as $file) {  
    $line .= "<td>". $file."</td>"; 
} 
foreach(glob($dir2) as $file) { 
    $line .= "<td>". $file."</td>"; 
} 
$line .= "</tr>"; 

echo $line; 
0

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

$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>"; 
} 
0

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