请问如下代码中
    $pagearr["name"]=$_name;
      $pagearr["size"]=$_size;
      $pagearr["time"]=$_time;
怎么通过foreach 取$pagearr的值呢?
我要循环到一个表格里面去、弄了两天啦
function page($f,$pagesize,$current)
{
$_name = array();
$_size = array();
$_time = array();

$total = ceil(count($f)/$pagesize);
$prev = (($current-1)<=0 ? "1":($current-1));
$next =($current+1>=$total ? $total:$current+1);
$current =($current>($total) ? ($total):$current);
$start = ($current-1)*$pagesize;
      
     

   for($i=$start;$i<($start+$pagesize);$i++)
   {
     if ($f!="." && $f!="..")
         {  
        
         $t = array_map('filemtime',$f);
           $s = array_map('filesize',$f); 
           array_multisort($t,SORT_DESC,$f);
          
          }
  
     array_push($_name,$f[$i]);
     array_push($_size,$s[$i]);
     array_push($_time,$t[$i]);
     
    }
    $pagearr["name"]=$_name;
      $pagearr["size"]=$_size;
      $pagearr["time"]=$_time;
    $pagearr["page"]="<a href=\"?acticon=barfilelist&id=".$_GET['id']."&page=1\">first</a> <a href=\"?acticon=barfilelist&id=".$_GET['id']."&page={$prev}\">prev</a> <a href=\"?acticon=barfilelist&id=".$_GET['id']."&page={$next}\">next</a> <a href=\"?acticon=barfilelist&id=".$_GET['id']."&page={$total}\">end</a>";
    return $pagearr;
}if ($handle = opendir('.\dbbak')) 
{ echo '<ul><b><li style="margin-top:20px;text-decoration:none;">客户列表:</b>
      <hr width=80% size=3 color=#0099ff style="FILTER:alpha(opacity=100,finishopacity=0,style=1)">
        <ul>';
  while ($file = readdir($handle)) {   
  if ($file!='.' && $file!="..")
  echo '<li><a target="select_result" href=readir.php?acticon=barfilelist&id='.$file.'>'.$file.'</a></li>';   
  }     
  echo  '</ul>
        </li>   
        </ul>';
  closedir($handle);   
}
break;case('barfilelist'):
$patch='./dbbak/';
if ($handle = opendir($patch.$_GET['id'])) 
       { 
        echo '<table id="tb1">
        <th width="80px">客户编号</th>
        <th width="200px">文件名</th>
        <th width="80px">文件大小</th>
        <th width="200px;">上传时间</th>
        <th width="30px">下载</th>
        </table>';
      }
      $f = array();
      $f =  glob($patch.$_GET['id'].'/*');
      
      if (isset($_GET['page']))
      {
       $page = $_GET['page'];
       }
      else
      {
       $page = 1;
       }                  
 $r = page($f,20,$page);
 foreach($r["name"] as $value){
 echo '<table id="tb2">
     
     <td width="80px">'.$_GET['id'].'</td>
     <td width="200px">'.substr($value,19).'</td>
     <td width="80px">KB</td>
     <td width="200px"></td>
     <td width="30px"><a href="http://localhost/'.$value.'"><img alt="点击下载" align="center" style="border:0;" src="./image/download2.gif" /></a></td>
      </table>'; 
 }
 
   echo '<div style="text-align:center;margin-top:25px;">
   '.$r["page"].'
 </div>';

解决方案 »

  1.   

    不太明白你想问什么,你的代码中不是已经用到foreach了吗?具体用法在手册中写得很明白啊。$pagearr["name"] = $_name;
    $pagearr["size"] = $_size;
    $pagearr["time"] = $_time;echo "<table>\n<tr>\n";foreach($pagearr as $k => $v) echo "<td>{$k}:{$v}</td>\n";echo "</tr>\n</table>\n";
      

  2.   

    但是在这个地方 $pagearr 应该是个多维数组了吧,是不是不能直接echo了?
      

  3.   

    还是仔细看看手册吧。foreach循环时,你可以在循环体中进行进一步处理啊(方法有很多,foreach、for循环,或者直接输出输出元素)。
    $pagearr["name"] = array('AAA', 'BBB', 'CCC');
    $pagearr["size"] = array('111', '222', '333');
    $pagearr["time"] = array('@@@', '###', '$$$');echo "<table>\n";foreach($pagearr as $k => $v) {
       echo "<tr>\n";
       for($i = 0; $i < count($v); $i ++) echo "<td>{$v[$i]}</td>\n";
       echo "</tr>\n";
       
       echo "<tr><td>{$v[2]}</td><td>{$v[1]}</td><td>{$v[0]}</td></tr>\n";
       
       echo "<tr>\n";
       foreach($v as $w) echo "<td>{$w}</td>\n";
       echo "</tr>\n";
    }echo "</table>\n";
      

  4.   

    我把你的代码运行了、得出的结果是AAA BBB CCC 
    CCC BBB AAA 
    AAA BBB CCC 
    111 222 333 
    333 222 111 
    111 222 333 
    @@@ ### $$$ 
    $$$ ### @@@ 
    @@@ ### $$$ 
    但是需要的结果应该是文件名  文件大小  访问时间
    AAA    111     @@@
    BBB    222     333
    @@@    ###     SSS
      

  5.   

    那就不该用foreach循环输出。
    $pagearr["name"] = array('AAA', 'BBB', 'CCC');
    $pagearr["size"] = array('111', '222', '333');
    $pagearr["time"] = array('@@@', '###', '$$$');echo "<table>\n";
    echo "<tr><td>文件名</td><td>文件大小</td><td>访问时间</td></tr>\n";for($i = 0; $i < 3; $i ++) echo "<tr><td>{$pagearr['name'][$i]}</td><td>{$pagearr['size'][$i]}</td><td>{$pagearr['time'][$i]}</td></tr>\n";echo "</table>\n";