doujing1858 2014-12-01 08:55
浏览 30
已采纳

php implode multidimensional array

I have an array, which started off as an empty array with

$filearray = array(array(array()));

I know how to do it if there were tag IDs set, but I seem to be struggling where I've built on it.

while (($file = readdir($dir)) !== false)
{
    //echo $file ;

    //gets the file date, string needed decoding otherwise throws error. Thanks to stackoverflow!

    $date = @filemtime($file);
    $date = date("F d Y H:i:s.", filemtime(utf8_decode("/home/a1742406/public_html/files/$file")));
    //echo $date. "<br />";

    $size = filesize("/home/a1742406/public_html/files/$file") . ' bytes';

    $filearray = array($file, $date, $size);

    print "<pre>";
    $data = implode(" ", $filearray);
    echo $data;
    //print_r($filearray);
    print "</pre>";
}

My filearray has 3 parts, file, date and size, but I want to print them separately so i can have spacing between them and put them into a table or list, but I can't seem to figure how to separate them to do it, as the above prints it all in one row. Any help would be greatly appreciated to point me in the right direction!

Thanks.

  • 写回答

3条回答 默认 最新

  • duanjian5059 2014-12-01 08:59
    关注

    If you plan on printing them inside the <pre> pre formatted tags, then you could just use :

    print "<pre>";
    $data = implode(" ", $filearray);
    echo $data . "
    "; // <-- new line, or if you want `
    
    `, whatever how much space you want between lines
    print "</pre>";
    

    If you also want file, date, and size in each separate lines, then you could just glue them with a newline:

    print "<pre>";
    $data = implode("
    ", $filearray);
    echo $data . "
    
    ";
    print "</pre>";
    

    If you plan on presenting them on a table, then just build up the markup like you normally would do:

    echo '<table cellpadding="10">';
    echo '
        <tr>
            <th>File</th>
            <th>Date</th>
            <th>Size</th>
        </tr>
    ';
    
    while (($file = readdir($dir)) !== false)
    {
        // other codes inside
    
        echo '<tr>';
            echo "<td>$file</td>";
            echo "<td>$date</td>";
            echo "<td>$size</td>";
        echo '</tr>';
    
    }
    echo '</table>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式