如何在数组结构中获取父级的所有子级

时间:2015-02-07 14:53:07

标签: php arrays

在mysql查询和一些转换后,我有以下数组:

$users = Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 
            [token] => ADCUN5EW5FAR
            [token_parent_id] => 
            [name] => NODE
            [payment] => 1
        )

    [1] => Array
        (
            [id] => 2
            [parent_id] => 1
            [token] => N9KWQQV1W1K5
            [token_parent_id] => ADCUN5EW5FAR
            [name] => NODE 1
            [payment] => 1
        )

    [2] => Array
        (
            [id] => 4
            [parent_id] => 2
            [token] => 5NAKJYKUD1X2
            [token_parent_id] => N9KWQQV1W1K5
            [name] => NODE 1-1
            [payment] => 0
        )

    [3] => Array
        (
            [id] => 5
            [parent_id] => 4
            [token] => 8EMAEK9XFRFZ
            [token_parent_id] => 5NAKJYKUD1X2
            [name] => NODE 1-1-1
            [payment] => 0
        )

    [4] => Array
        (
            [id] => 6
            [parent_id] => 4
            [token] => Z9XFZ2EZ38VR
            [token_parent_id] => 5NAKJYKUD1X2
            [name] => NODE 1-1-2
            [payment] => 0
        )

    [5] => Array
        (
            [id] => 3
            [parent_id] => 1
            [token] => R751DRTJ1EKW
            [token_parent_id] => ADCUN5EW5FAR
            [name] => NODE 2
            [payment] => 0
        )

    [6] => Array
        (
            [id] => 7
            [parent_id] => 3
            [token] => DR6NGPDAZN25
            [token_parent_id] => R751DRTJ1EKW
            [name] => NODE 2-1
            [payment] => 0
        )

);

如果我有一个给定的id,我需要为id下的项创建以下数组结构,例如,如果给定的Id是1,我需要数组:

 Array
(
    [ADCUN5EW5FAR] => Array
        (
            [node] => Array
                (
                    [id] => 1
                    [parent_id] => 
                    [token] => ADCUN5EW5FAR
                    [token_parent_id] => 
                    [name] => NODE
                    [payment] => 1
                )

            [children] => Array
                (
                    [N9KWQQV1W1K5] => Array
                        (
                            [node] => Array
                                (
                                    [id] => 2
                                    [parent_id] => 1
                                    [token] => N9KWQQV1W1K5
                                    [token_parent_id] => ADCUN5EW5FAR
                                    [name] => NODE 1
                                    [payment] => 1
                                )

                            [children] => Array
                                (
                                    [5NAKJYKUD1X2] => Array
                                        (
                                            [node] => Array
                                                (
                                                    [id] => 4
                                                    [parent_id] => 2
                                                    [token] => 5NAKJYKUD1X2
                                                    [token_parent_id] => N9KWQQV1W1K5
                                                    [name] => NODE 1-1
                                                    [payment] => 0
                                                )

                                            [children] => Array
                                                (
                                                    [8EMAEK9XFRFZ] => Array
                                                        (
                                                            [node] => Array
                                                                (
                                                                    [id] => 5
                                                                    [parent_id] => 4
                                                                    [token] => 8EMAEK9XFRFZ
                                                                    [token_parent_id] => 5NAKJYKUD1X2
                                                                    [name] => NODE 1-1-1
                                                                    [payment] => 0
                                                                )

                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                    [Z9XFZ2EZ38VR] => Array
                                                        (
                                                            [node] => Array
                                                                (
                                                                    [id] => 6
                                                                    [parent_id] => 4
                                                                    [token] => Z9XFZ2EZ38VR
                                                                    [token_parent_id] => 5NAKJYKUD1X2
                                                                    [name] => NODE 1-1-2
                                                                    [payment] => 0
                                                                )

                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [R751DRTJ1EKW] => Array
                        (
                            [node] => Array
                                (
                                    [id] => 3
                                    [parent_id] => 1
                                    [token] => R751DRTJ1EKW
                                    [token_parent_id] => ADCUN5EW5FAR
                                    [name] => NODE 2
                                    [payment] => 0
                                )

                            [children] => Array
                                (
                                    [DR6NGPDAZN25] => Array
                                        (
                                            [node] => Array
                                                (
                                                    [id] => 7
                                                    [parent_id] => 3
                                                    [token] => DR6NGPDAZN25
                                                    [token_parent_id] => R751DRTJ1EKW
                                                    [name] => NODE 2-1
                                                    [payment] => 0
                                                )

                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

我需要编写代码才能获得这个新数组,从一个不同于1的id开始但使用原始的$ users数组。愿有人帮帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

这不是很漂亮,但仍然紧凑而且有效:

 $users = array(
     array
        (
            'id' => 1,
            'parent_id' => '',
            'token' => 'ADCUN5EW5FAR',
            'token_parent_id' =>'' ,
            'name' => 'NODE',
            'payment' => 1 
        ),

    array
        (
            'id' => 2,
            'parent_id' => 1,
            'token' => 'N9KWQQV1W1K5',
            'token_parent_id' =>'ADCUN5EW5FAR' ,
            'name' => 'NODE 1',
            'payment' => 1 
        ),

    array
        (
            'id' => 4,
            'parent_id' => 2,
            'token' => '5NAKJYKUD1X2',
            'token_parent_id' =>'N9KWQQV1W1K5' ,
            'name' => 'NODE 1-1',
            'payment' => 0 
        ),

    array
        (
            'id' => 5,
            'parent_id' => 4,
            'token' => '8EMAEK9XFRFZ',
            'token_parent_id' =>'5NAKJYKUD1X2' ,
            'name' => 'NODE 1-1-1',
            'payment' => 0 
        ),

    array
        (
            'id' => 6,
            'parent_id' => 4,
            'token' => 'Z9XFZ2EZ38VR',
            'token_parent_id' =>'5NAKJYKUD1X2' ,
            'name' => 'NODE 1-1-2',
            'payment' => 0 
        ),

    array
        (
            'id' => 3,
            'parent_id' => 1,
            'token' => 'R751DRTJ1EKW',
            'token_parent_id' =>'ADCUN5EW5FAR' ,
            'name' => 'NODE 2',
            'payment' => 0 
        ),

    array
        (
            'id' => 7,
            'parent_id' => 3,
            'token' => 'DR6NGPDAZN25',
            'token_parent_id' =>'R751DRTJ1EKW' ,
            'name' => 'NODE 2-1',
            'payment' => 0 
        )

);

function getBranches($arr, $id) {
    $childrenArr = array();
    foreach($arr as $item) {
        if ($item['parent_id']==$id) {
            $childrenArr[] = $item;
        }
    }
    return $childrenArr;
}

function getBranch($arr, $id) {
    $branch = array();
    foreach($arr as $item) {

        if ($item['id'] == $id) {
            $branch[$item['token']]['node'] = $item;
            $branches = getBranches($arr, $id);
            $children = array();
            foreach($branches as $child) {
                $b = getBranch($arr, $child['id']);
                foreach ($b as $token => $child) {
                    $children[$token] = $child;
                }
            }
            $branch[$item['token']]['children'] = $children;
            break;
        }
    }
    if (count($branch)==0) echo 'WARNING '.$id;
    return $branch;
}

print_r(getBranch($users,1));
print_r(getBranch($users,4));