在PHP中从平面数组创建嵌套UL

时间:2014-08-21 18:58:55

标签: php arrays nested-lists

这是一个从JSON文件构建的数组。我想要做的是创建一个无序的嵌套列表。我已经看过很多教程,但它们通常只适用于简单(id,父ID,名称)布局。这个阵列比我的尝试似乎不起作用的原因要复杂得多。

这是理想的结果:

  • 标准I:课程,规划和评估
    • 指标I-A。课程与规划
      • I-A-1。儿童和青少年发展
        • 内容将在此处
      • I-A-2。儿童和青少年发展
        • 内容将在此处
    • 此处有与标准I
    • 相关的更多指标
  • 标准II:....

有多个父母,他们的ID由*_id字段分隔。我包含了具有不同名称的重复字段,以允许基于我在网上看到的可以执行$parentID == $id之类的操作的示例进行比较。我正在研究如何将其转换为树阵列以使其更容易阅读,但也遇到了类似的复杂问题。

所以要理解下面的结构,这里有一个关键:

[top_id] = [standard]的ID,出于比较原因与[top]相同

[parent_id] = [indicators]的ID,出于比较原因与[parent]相同

[child_id] = [element]的ID,出于比较原因与[parent]相同

其他是与[元素]相关联的内容,一旦我成功创建列表,我就可以显示这些内容。

    Array
    (
    [0] => Array
        (
            [top_id] => 1
            [top] => 1
            [parent_id] => 2
            [parent] => 2
            [child_id] => 5
            [child] => 5
            [standard] => Standard I: Curriculum, Planning, and Assessment
            [indicator] => Indicator I-A.   Curriculum & Planning
            [element] => I-A-1. Child and Adolescent Development
            [Connections] => some content here
            [Effective Practice] => some content here
            [Proficient] => some content here
            [Suggested Artifacts] => some content here
    )

    [1] => Array
        (
            [top_id] => 1
            [top] => 1
            [parent_id] => 2
            [parent] => 2
            [child_id] => 6
            [child] => 6
            [standard] => Standard I: Curriculum, Planning, and Assessment
            [indicator] => Indicator I-A.   Curriculum & Planning
            [element] => I-A-2. Child and Adolescent Development
            [Connections] => some content here
            [Effective Practice] => some content here
            [Proficient] => some content here
            [Suggested Artifacts] => some content here
        )

    )

- 尝试更新示例 -

foreach ($nodes as $node => $v) { 

    // id's
    $topID = $v['top_id'];
    $parentID = $v['parent_id'];
    $childID = $v['child_id'];
    $top = $v['top'];
    $parent = $v['parent'];
    $child = $v['child'];;

    // name values
    $standard = $v['standard'];
    $indicator= $v['indicator'];
    $element = $v['element'];
    $connections = $v['Connections'];
    $practice = $v['Effective Practice'];
    $proficient = $v['Proficient'];
    $artifacts = $v['Suggested Artifacts'];

    echo "<ul>";

    foreach($standard as $value){
        echo "<li>";
        print $value;
        echo "<ul>";

        foreach($indicator as $v){
            echo "<li>";
            print $v;
            echo "</li>";
        }
        echo "</ul>";
        echo "</li>";
    }
    echo '</ul>';
}

另外

    if ($node[$top][] == $topID[]){
        echo "<li>";
        print $standard;
        echo "<ul>";
        if ($node[$parent][] == $parentID[]){
            echo "<li>";
            print $indicator;
            echo "</li>";
        }
        echo "</ul>";   
        echo "</li>";
    }

1 个答案:

答案 0 :(得分:0)

我会收集数据并首先构建一个树,然后将树打印出来。一些示例代码:

foreach ($nodes as $item) {
    // gather the data in an assoc array indexed by id
    if ( !isset($data_by_id[ $item['top_id'] ])) {
        $data_by_id[ $item['top_id'] ] = array( 'name' => $item['standard'] );
    }
    if ( !isset($data_by_id[ $item['parent_id'] ])) {
        $data_by_id[ $item['parent_id'] ] = array( 'name' => $item['indicator'] );
    }
    if ( !isset($data_by_id[ $item['child_id'] ])) {
        $data_by_id[ $item['child_id'] ] = array( 
            'name' => $item['element'],
            'contents' => array(
                $item['Connections'],
                $item['Effective Practice'],
                $item['Proficient'],
                $item['Suggested Artifacts'])
        );
    }
    // construct the tree - I've made a simple three tier array
    $tree[ $item['top_id'] ][ $item['parent_id'] ][ $item['child_id'] ]++;
}

// go through the tree and print the info
// this is a recursive function that can be used on arbitrarily deep trees
function print_tree( $data, $arr ){
    echo "<ul>\n";
    // the tree is an associative array where $key is the ID of the node,
    // and $value is either an array of child nodes or an integer.
    foreach ($arr as $key => $value) {
        echo "<li>" . $data[$key]['name'] . "</li>\n";
        if (isset($data[$key]['contents']) && is_array($data[$key]['contents'])) {
            echo '<ul>';
            foreach ($data[$key]['contents'] as $leaf) {
                echo '<li>' . $leaf . "</li>\n";
            }
            echo "</ul>\n";
        }
        // if $value is an array, it is a set of child nodes--i.e. another tree.
        // we can pass that tree to our print_tree function.
        if (is_array($value)) {
            print_tree($data, $value);
        }
    }
    echo "</ul>\n";
}

print_tree($data_by_id, $tree);

你需要添加错误检查,删除奇怪的字符,删除多余的空格等。