dongmieqiao3152 2012-06-26 18:45
浏览 124
已采纳

在php中将数组显示为树结构

I have an array:

Array
(
    [0] => dir0|file0.txt
    [1] => dir0|file1.txt
    [2] => dir1|file2.txt
    [3] => dir1|filea.txt
    [4] => dir2|fileb.txt
)

I would like it to be displayed as a tree, such as:

dir0
  file0.txt
  file1.txt
dir1 
  file2.txt
  filea.txt
dir2
  fileb.txt

Can any one explain how I can do that?

Edit: Updated for multidimentional array:

$paths[0][0] = 'dir0|file0.txt';
$paths[0][1] = 400;
$paths[1][0] = 'dir0|filea.txt';
$paths[1][1] = 500;
$paths[2][0] = 'dir1|file1.txt';
$paths[2][1] = 600;
$paths[3][0] = 'dir1|fileb.txt';
$paths[3][1] = 700;
$paths[4][0] = 'dir2|filec.txt';
$paths[4][1] = 700;

I would like the output to be:

dir0 (900)
  file0.txt (400)
  filea.txt (500)
dir1 (1300)
  file1.txt(600)
  fileb.txt (700)
dir2 (700)
  filec.txt (700)

The values need to be added and displayed in root.

  • 写回答

2条回答 默认 最新

  • drpjdfj618393 2012-06-26 18:48
    关注

    The best way would be to reformat the array so the keys were the directories, and the array values were arrays containing file names, like so:

    $array = array( ..);
    $reformatted = array();
    foreach( $array as $k => $v) {
        list( $key, $value) = explode( '|', $v);
        if( !isset( $reformatted[$key])) 
            $reformatted[$key] = array();
        $reformatted[$key][] = $value;
    }
    

    Then you just have to iterate over the new array, like so:

    foreach( $reformatted as $dir => $files) {
        echo $dir . "
    ";
        foreach( $files as $file)
            echo "\t" . $file . "
    ";
    }
    

    This outputs:

    dir0
        file0.txt
        file1.txt
    dir1
        file2.txt
        filea.txt
    dir2
        fileb.txt
    

    Note that this will only work for plain text environment (such as <pre></pre>, like in the demo). Otherwise, you'll need to use <br /> instead of for line breaks, or use an ordered or unordered list.

    For HTML output, use this, whose output can be seen here

    echo '<ul>';
    foreach( $reformatted as $dir => $files) {
        echo "<li>$dir</li>";
        echo '<ul>';
        foreach( $files as $file)
            echo "<li>$file</li>";
        echo '</ul>';
    }
    echo '</ul>';
    

    Generates:

    • dir0
      • file0.txt
      • file1.txt
    • dir1
      • file2.txt
      • filea.txt
    • dir2
      • fileb.txt

    For your updated array, here is the solution:

    $reformatted = array(); $weights = array();
    foreach( $paths as $k => $v) {
        list( $key, $value) = explode( '|', $v[0]);
    
        if( !isset( $reformatted[$key])) 
            $reformatted[$key] = array();
    
        if( !isset( $weights[$key]))
            $weights[$key] = 0;
    
        $reformatted[$key][] = array( $value, $v[1]);
        $weights[$key] += $v[1];    
    }
    
    foreach( $reformatted as $dir => $files) {
        echo $dir . ' (' . $weights[$dir] . ")
    ";
        foreach( $files as $file)
            echo "\t" . $file[0] . ' (' . $file[1] . ")
    ";
    }
    

    This outputs:

    dir0 (900)
        file0.txt (400)
        filea.txt (500)
    dir1 (1300)
        file1.txt (600)
        fileb.txt (700)
    dir2 (700)
        filec.txt (700)
    

    I'll leave it up to you to translate that into HTML if necessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?