首页 > 解决方案 > 合并父子数组

问题描述

我有 PHP 数组。我正在尝试转换为父子数组:我使用了递归函数但没有得到我的输出。

Array
(


    [0] => Array
        (
            [id] => 1
            [subscription_name] => Yearly 
            [parent_id] => 0
        )



    [1] => Array
        (
            [id] => 22
            [subscription_name] => Yearly new               
            [parent_id] => 1
        )

    [2 => Array
        (
            [id] => 23
            [subscription_name] => Yearly new offer 
            [parent_id] => 22
        )

    [3] => Array
        (
            [id] => 24
            [subscription_name] => Weekly
            [parent_id] => 0
        )

    [4] => Array
        (
            [id] => 25
            [subscription_name] => Weekly new offer
            [parent_id] => 24
        )

)

我期待这个结果

Array
(
    [0] => Array
        (
            [id] => 1
            [subscription_name] => Yearly new offer 
            [childrens] => Array
                (
                    [0] => Array
                        (
                            [id] => 22
                        )
                    [1] => Array
                        (
                            [id] => 23
                        )   
                )

        )

    [1] => Array
        (
            [id] => 24
            [subscription_name] => Weekly new offer 
            [childrens] => Array
                (
                    [0] => Array
                        (
                            [id] => 25
                        )

                )

        )

)

我已经尝试过但没有得到我想要的输出

我的 PHP 函数是

function tree(array $elements, $parent_id = 0) {
      echo "<pre>";
        $branch = array();
        $a=array();
        foreach ($elements as $element) {
           if ($element['parent_id'] == $parent_id && $element['subscription_type_id'] !=1 ) {
                $children = $this->tree($elements, $element['id']);
                if ($children) {
                    $element['children'] = $children;
                }
                else {
                    $element['children'] = array();
                }
                $branch[] = $element;
            }

        }
        return $branch;
    }

这是我从上述函数得到的输出:

Array
(
    [0] => Array
    (
        [id] => 1
        [subscription_name] => Yearly 
        [children] => Array
        (
                [0] => Array
                    (
                        [id] => 22
                        [subscription_name] => Yearly new 
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 23
                                        [subscription_name] => Yearly new offer 
                                        [children] => Array
                                            (
                                            )

                                    )

                        )

                )

    )

)

[1] => Array
(
    [id] => 24
    [subscription_name] => Weekly
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 25
                    [subscription_name] => Weekly new offer 
                    [children] => Array
                        (
                        )

                )

        )

)

请帮我解决这个问题。谢谢。

标签: phparrayscodeigniterloopsparent-child

解决方案


真的,我的意思是非常幼稚的版本,但顺便说一句,你的数据真的更容易被利用,比如parent_id如果它不是父 ID(例如 NULL)就没有任何到 0 我也不明白你为什么有不同subscription_name。我认为你应该更多地改变你的输入,而不是找到一个复杂的算法来解析它们

function tree(array $elements) {
    $openList = [];
    $result = [];
    $id = 0;
    foreach ($elements as $key => $element) {
        if($key != 0) // I suppose the trial one is not used
        {
            if($element['parent_id'] == 0) // a root
            {
                $closeList = [];
                $openList[$element['id']] = $element['id'];
                $result[$id] = $element;
                unset($result[$id]['parent_id']);
                $result[$id]['children'] = [];
                $newOpenlist = [];
                while(count($openList) > 0)
                {
                    foreach ($elements as $key => $element) {
                        if($element['parent_id'] != 0 && in_array($element['parent_id'], $openList) && !in_array($element['parent_id'], $closeList))
                        {
                            $newOpenlist[$element['id']] = $element['id'];
                            $result[$id]['children'][] = $element;
                        }
                    }
                    foreach($openList as $item)
                    {
                        $closeList[] = $item;
                    }
                    $openList = $newOpenlist;
                    $newOpenlist = [];
                }
            }
            $id++;
        }
    }
    return $result;
}

推荐阅读