Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP recursive array: display parent and child correctly

Tags:

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 Answers

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());
?>
like image 165
Marcel Kohls Avatar answered Oct 18 '25 06:10

Marcel Kohls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!