PHP递归数组:正确显示父和子

时间:2018-05-01 23:33:56

标签: php

我想让父母成为新的“ul”和孩子“li”项目,但显然父母也必须同时是“li”和“ul”,bcz我想建立“父母子女关系列表“使用树原则,child也将成为另一个元素的父级,这将创建另一个子类别等等......那么你将如何正确地完成它?如果你不明白的话......问。在图像中我只显示它应该是什么样子,简单的树enter image description here

<?php
$tasks[] = array("id" => 1, "parent_id" => 0, "title" => 'task 1');
$tasks[] = array("id" => 2, "parent_id" => 1, "title" => 'sub task 1');
$tasks[] = array("id" => 3, "parent_id" => 1, "title" => 'sub task 2');
$tasks[] = array("id" => 4, "parent_id" => 2, "title" => 'sub sub task 1');
$tasks[] = array("id" => 5, "parent_id" => 2, "title" => 'task 2');
$tasks[] = array("id" => 6, "parent_id" => 2, "title" => 'sub task 3');
$branch = array();
function buildTree(array &$elements, $parentId = 0) {
    $branch = array();
    foreach ($elements as &$element) {
        if ($element['parent_id'] == $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
                // $element['parent']=$element['title'];
                print_r($element['id']);
                foreach($element['children'] as $child){
                    if($element['id']==$child['parent_id']){
                    echo '<ul>';
                    echo '<li>'.$child['title'].'</li>';
                    echo '</ul>';
                    }
                }
            }
            $branch[$element['id']] = $element;
        }
    }
    return $branch;
}
print_r(buildTree($tasks));
?>

1 个答案:

答案 0 :(得分:0)

您应该尝试对项目的孩子进行分组,这样您就可以确定哪些项目有父亲&#34;父亲&#34;哪个没有。我用您的代码制作了一个示例,但您也可以使用array_filter来改进它:

<?php
    $tasks[] = array("id" => 1, "parent_id" => 0, "title" => 'task 1');
    $tasks[] = array("id" => 2, "parent_id" => 1, "title" => 'sub task 1');
    $tasks[] = array("id" => 3, "parent_id" => 1, "title" => 'sub task 2');
    $tasks[] = array("id" => 5, "parent_id" => 2, "title" => 'task 2');
    $tasks[] = array("id" => 4, "parent_id" => 2, "title" => 'sub sub task 1');
    $tasks[] = array("id" => 6, "parent_id" => 2, "title" => 'sub task 3');
    $tasks[] = array("id" => 7, "parent_id" => 6, "title" => 'sub task of 6');
    $branch = array();

    function buildTree(array $elements, array $branch, $parentId=0) {
        // group elements by parents if it does not comes on the parameters
        if (empty($branch)) {
            $branch = array();

            foreach ($elements as $element) {
                $branch[$element["parent_id"]][$element["id"]] = $element;
            }
        }

        // echo the childs referenced by the parentId parameter
        if (isset($branch[$parentId])) {
            echo'<ul>';

            foreach ($branch[$parentId] as $keyBranch => $itemBranch) {
                echo '<li>'.$itemBranch['title'];
                buildTree($elements, $branch, $itemBranch["id"]); // iterate with the actual Id to check if this record have childs
                echo '</li>';
            }

            echo '</ul>';
        }
    }

    buildTree($tasks, array());
?>