PHP分层数组-父母与子女

时间:2019-12-05 13:53:55

标签: php arrays algorithm loops sorting

我正在创建一个父级-子级层次结构数组。请在数组下面找到并帮助我修复。

依次使用parent_id时,效果很好,但是我想保持数组值不变。

function Testing()
{
    $tasks[] = array("id" => 1, "parent_id" => 0);
    $tasks[] = array("id" => 2, "parent_id" => 5);
    $tasks[] = array("id" => 3, "parent_id" => 2);
    $tasks[] = array("id" => 5, "parent_id" => 3);
    $tasks[] = array("id" => 4, "parent_id" => 3);
    $tasks[] = array("id" => 6, "parent_id" => 5);
    $tasks[] = array("id" => 7, "parent_id" => 6);

    $tree = buildTree($tasks);
    print("<pre>");print_r($tree);
}

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;
            }
            $branch[] = $element;
        }
    }

    return $branch;
}

预期产量

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [parent_id] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [parent_id] => 2
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 5
                                                            [parent_id] => 3
                                                            [children] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [id] => 6
                                                                            [parent_id] => 5
                                                                            [children] => Array
                                                                                (
                                                                                    [0] => Array
                                                                                        (
                                                                                            [id] => 7
                                                                                            [parent_id] => 6
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 4
                                                            [parent_id] => 3
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

1 个答案:

答案 0 :(得分:0)

为避免发生无限循环错误,当某些元素的子代具有父代ID时,可以采用第三个参数列出所有已管理的ID。而且,如果您的治疗返回ID,您可能会抛出异常:

function buildTree(array $elements, $parentId = 0, $managedParent = []) {
    $branch = array();

    // Add new id in list of managed ids
    $managedParent[] =$parentId;

    foreach ($elements as $element) {

        if ($element['parent_id'] == $parentId) {

          // check id not already managed
          if(in_array($element['id'], $managedParent))
          {
            // in this case children are already a parent of same structure. Throw an Excpetion
            throw new \Exception('Invalid structure given');
          }

          $children = buildTree($elements, $element['id'], $managedParent);
          if ($children) {
              $element['children'] = $children;
          }
          $branch[] = $element;
        }
    }

    return $branch;
}

编辑:

实时示例here