dousi6087 2018-05-01 23:33
浏览 186
已采纳

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

I want to make parent to be new "ul" and child "li" item,but obviously parent will have to be "li" and "ul" too at the same time,bcz i want to build "parent child relationship list" with tree principle, child will be a parent of another element too and that will create another subcategory and etc... so how would you have done it correctly? if you couldn't understand something... ask. In image i just display how it should look like,simple treeenter 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条回答 默认 最新

  • dongwalun2507 2018-05-02 00:57
    关注

    You should try to group the childs of the items, so you can identify which items has "fathers" and which not. I made a sample with your code, but you can also some array_filter to improve it:

    <?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());
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度