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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?