Creating a nested UL from flat array in PHP

醉酒当歌 提交于 2020-01-14 03:26:08

问题


This is an array that was built from a JSON file. 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
    • Indicator I-A. Curriculum & Planning
      • I-A-1. Child and Adolescent Development
        • content will go in here
      • I-A-2. Child and Adolescent Development
        • content will go in here
    • More indicators here that are related to Standard I
  • Standard II: ....

There are multiple parents, and their IDs are separated by the *_id field. 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. 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

[parent_id] = [indicators]'s ID and is the same as [parent] for comparison reasons

[child_id] = [element]'s ID and is the same as [parent] for comparison reasons

The others are content associated to the [element] which I can get to show up once I get my list created successfully.

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

回答1:


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.



来源:https://stackoverflow.com/questions/25433974/creating-a-nested-ul-from-flat-array-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!