简体   繁体   English

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

[英]Creating a nested UL from flat array in PHP

This is an array that was built from a JSON file. 这是从JSON文件构建的数组。 What I want to do is create a unordered nested list. 我想做的是创建一个无序的嵌套列表。 I have seen a lot of tutorials out there but they usually only work for a simple (id, parent id, name) layout. 我在那里看到了很多教程,但它们通常仅适用于简单的(标识,父标​​识,名称)布局。 This array is more complicated than that which is why my attempts don't seem to work. 这个数组比之复杂,这就是为什么我的尝试似乎不起作用的原因。

This is the desired outcome: 这是理想的结果:

  • Standard I: Curriculum, Planning, and Assessment 标准I:课程,计划和评估
    • Indicator IA. 指标IA。 Curriculum & Planning 课程与计划
      • IA-1. IA-1。 Child and Adolescent Development 儿童和青少年发展
        • content will go in here 内容会放在这里
      • IA-2. IA-2。 Child and Adolescent Development 儿童和青少年发展
        • content will go in here 内容会放在这里
    • More indicators here that are related to Standard I 这里有更多与标准I相关的指标
  • Standard II: .... 标准II:....

There are multiple parents, and their IDs are separated by the *_id field. 有多个父级,并且其ID由*_id字段分隔。 I included duplicate fields, with different names, to allow a comparison based off the examples I saw online that could do something like $parentID == $id . 我包括了重复的字段,使用不同的名称,以允许根据我在网上看到的示例进行比较,这些示例可以执行类似$parentID == $id I was looking into ways of converting this to a tree array to make reading it easier, but ran into similar complications there too. 我一直在研究将其转换为树形数组的方法,以使其阅读起来更容易,但在那里也遇到了类似的麻烦。

So to understand the structure below, here is a key: 因此,要了解下面的结构,这是一个关键:

[top_id] = [standard] 's ID and is the same as [top] for comparison reasons [top_id] = [standard]的ID,并且出于比较原因与[top]相同

[parent_id] = [indicators] 's ID and is the same as [parent] for comparison reasons [parent_id] = [indicators]的ID,出于比较原因与[parent]相同

[child_id] = [element] 's ID and is the same as [parent] for comparison reasons [child_id] = [element]的ID,出于比较原因与[parent]相同

The others are content associated to the [element] which I can get to show up once I get my list created successfully. 其他是与[element]相关的内容,一旦成功创建列表,我就可以显示该内容。

    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
        )

    )

-- UPDATE WITH ATTEMPT EXAMPLES -- -尝试示例更新-

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>';
}

Also

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

I would gather the data and construct a tree first, and then print the tree out. 我会先收集数据并构造一棵树,然后将其打印出来。 Some sample code: 一些示例代码:

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);

You'll need to add in error checking, zapping of strange characters, removal of excess whitespace, etc. 您需要添加错误检查,更改奇怪字符,清除多余的空格等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM