Create nested list from Multidimensional Array

后端 未结 3 1166
礼貌的吻别
礼貌的吻别 2020-12-03 08:31

I have an array in PHP, which looks like this:

array (
    [0] => array (
        [id] => 1
        [title] => \"Title 1\"
        [parent_id] =>         


        
相关标签:
3条回答
  • 2020-12-03 09:14

    The following array:

    Array
    (
        [0] => Content
        [1] => Array
            (
                [0] => International
                [1] => Array
                    (
                        [0] => Mexico
                        [1] => Array
                            (
                                [0] => Tamaulipas
                            )
    
                        [2] => USA
                    )
    
            )
    
    )
    

    With this function:

    function r($element) {
        foreach ($element as $value) {
            if (!is_array($value)) {
                echo "<li>";
                echo $value;
            } else {
                echo "<ul>";
                r($value);
                echo "</li>";
                echo "</ul>";
            }
        }
    }
    

    PHP code:

    echo "<ul>";
    r($array);
    echo "</ul>";
    

    Returns:

    <ul>
        <li>Public</li>
        <li>User</li>
        <li>Content
            <ul>
                <li>International
                    <ul>
                        <li>Mexico
                            <ul>
                                <li>Tamaulipas</li>
                            </ul>  
                        </li>
                        <li>USA</li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
    
    0 讨论(0)
  • 2020-12-03 09:19

    You should use recursion:

    First the array in 'php' syntax:

    <?php
    $a=array (
        '0' => array (
            'id' => 1,
            'title' => "Title 1",
            'parent_id' => 'NULL',
            'depth' => 0
        ),
        '1' => array (
            'id' => 2,
            'title' => "Title 2",
            'parent_id' => 'NULL',
            'depth' => 0
        ),
        '2' => array (
            'id' => 3,
            'title' => "Title 3",
            'parent_id' => 2,
            'depth' => 1
        ),
        '3' => array (
            'id' => 4,
            'title' => "Title 4",
            'parent_id' => 2,
            'depth' => 1
        ),
        '4' => array (
            'id' => 5,
            'title' => "Title 5",
            'parent_id' => 'NULL',
            'depth' => 0
        ),
        '5' => array (
            'id' => 6,
            'title' => "Title 6",
            'parent_id' => 4,
            'depth' => 0
        )
    );
    

    Here the code:

    $level = 'NULL';
    
    function r( $a, $level) {
       $r = "<ol>";
       foreach ( $a as $i ) {
           if ($i['parent_id'] == $level ) {
              $r = $r . "<li>" . $i['title'] . r( $a, $i['id'] ) . "</li>";
           }
       }
       $r = $r . "</ol>";
       return $r;
    }
    
    print r( $a, $level );
    
    ?>
    

    The results:

    <ol><li>Title 1<ol></ol></li><li>Title 2<ol><li>Title 3<ol>
    </ol></li><li>Title 4<ol><li>Title 6<ol></ol></li></ol></li></ol></li><li>Title 5
    <ol></ol></li></ol>
    
    1. Title 1\n
      1. Title 2\n
        1. Title 3\n
          1. Title 4\n
            1. Title 6\n
          2. Title 5\n

            EDITED AFTER CHECK AS SOLUTION

            To avoid empty leafs:

            function r( $a, $level) {
               $r = '' ;
               foreach ( $a as $i ) {
                   if ($i['parent_id'] == $level ) {
                      $r = $r . "<li>" . $i['title'] . r( $a, $i['id'] ) . "</li>";
                   }
               }
               return ($r==''?'':"<ol>". $r . "</ol>");
            }
            
            0 讨论(0)
          3. 2020-12-03 09:20

            You can try the following

            $array = array (
                "0" => array (
                    "id" => 1,
                    "title" => "Title 1",
                    "parent_id" => NULL,
                    "depth" => 0
                ),
                "1" => array (
                    "id" => 2,
                    "title" => "Title 2",
                    "parent_id" => NULL,
                    "depth" => 0
                ),
                "2" => array (
                    "id" => 3,
                    "title" => "Title 3",
                    "parent_id" => 2,
                    "depth" => 1
                ),
                "3" => array (
                    "id" => 4,
                    "title" => "Title 4",
                    "parent_id" => 2,
                    "depth" => 1
                ),
                "4" => array (
                    "id" => 5,
                    "title" => "Title 5",
                    "parent_id" => NULL,
                    "depth" => 0
                ),
                "5" => array (
                    "id" => 6,
                    "title" => "Title 6",
                    "parent_id" => 4,
                    "depth" => 0
                )
            );
            
            echo(make($array));
            

            Output

            <ol>
                <li>Title 1</li>
                <li>Title 2</li>
                <ol>
                    <li>Title 3</li>
                    <li>Title 4</li>
                    <ol>
                        <li>Title 6</li>
                    </ol>
                </ol>
                <li>Title 5</li>
            </ol>
            

            Function Used

            function make(array $array, $no = 0) {
                $child = hasChildren($array, $no);
                if (empty($child))
                    return "";
                $content = "<ol>\n";
                foreach ( $child as $value ) {
                    $content .= sprintf("\t<li>%s</li>\n", $value['title']);
                    $content .= make($array, $value['id']);
                }
                $content .= "</ol>\n";
                return $content;
            }
            
            function hasChildren($array, $id) {
                return array_filter($array, function ($var) use($id) {
                    return $var['parent_id'] == $id;
                });
            }
            

            See Live Demo

            0 讨论(0)
          提交回复
          热议问题