Php 如何将两个foreach循环合并为一个
这是我的代码到目前为止,我想结合这一点,我能做什么Php 如何将两个foreach循环合并为一个,php,arrays,Php,Arrays,这是我的代码到目前为止,我想结合这一点,我能做什么 <table> <tr> <th>Imported Files</th> <th>Report Files</th> </tr> <?php $dir = str_replace("/var/www/13_Feb/subscrib
<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>
我想把这个打印到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]");
$line.=”;
foreach(glob($dir)作为$file){
$line.=''.$file.'';
}
foreach(glob($dir2)作为$file){
$line.=''.$file.'';
}
$line.=”;
回声线;
这个可以处理不同大小的文件列表
另外,您可以在同一个表中显示两列以上的“文件列表”
// 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>';
//检索数据
$dirs=数组(glob($dir),glob($dir2));
//展示
$max_files=max(数组_映射('count',$dirs));
$count_dirs=计数($dirs);
回声';
对于($i=0;$i<$max_文件;$i++)
{
回声';
对于($d=0;$d<$count_dirs;$d++)
{
$strFile=(isset($dirs[$d][$i])?$dirs[$d][$i]:“[NO FILE]”);
回显“.$strFile.”;
}
回声';
}
回声';
如果它们应该组合在一起并且大小相同,那么这可能是一个解决方案(未经测试)
$dir1\u files=glob($dir);
$dir2_files=glob($dir2);
对于($i=0;$i
PHP5.3+对此有一个解决方案,再加上您可以有这样一个解决方案:
$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>";
}
每个目录中的文件数是否相同?您是说要在dir2中的第一个文件旁边显示dir中的第一个文件吗?我该怎么办呢?:)你的代码在任何测试页面上对我来说都很好,但在我替换str_replace时实际上不与zend一起工作,之后它在zend中也可以工作,我可以做什么建议?你为什么不简单地添加
chdir(/var/www/13_Feb/subscriber files”)代码>在处理之前?然后你可以做$dir='u*[cC][sS][vV]代码>等等,然后,glob()
调用。。。顺便说一句,如果你对我的答案感到满意,请不要忘记接受/投票:)
$line .= "<tr>";
foreach(glob($dir) as $file) {
$line .= "<td>". $file."</td>";
}
foreach(glob($dir2) as $file) {
$line .= "<td>". $file."</td>";
}
$line .= "</tr>";
echo $line;
// 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>';
$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>";
}
$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>";
}
$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] : ' ') ."</td>";
echo "<td>". (isset($current[1]) ? $current[1] : ' ') ."</td>";
echo "</tr>";
}