使用递归PHP计算父数组

时间:2017-02-21 10:56:27

标签: php arrays recursion

如何使用递归计算父数组..

这是我的数组

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]        => 4
                                [parent_id] => 3,
                            ),

                        ),

                    ),

                ),

            )

            [1] => array
            (
                [id]        => 5
                [parent_id] => 1,
            ),

        ),

    )

    [1] => array
    (
        [id]        => 1
        [parent_id] => 0
        [children]  => array
        (
            [id]        => 2
            [parent_id] => 1
            [children]  => array
            (
                [id]        => 3
                [parent_id] => 2,
            ),

        ),

    )

    [2] => array
    (
        [id]        => 1
        [parent_id] => 0,
    ),

)

结果是我想要的东西......

array
(
    [0] => array
    (
        [id]           => 1
        [parent_id]    => 0
        [parent_count] => 0
        [children]     => array
        (
            [0] => array
            (
                [id]           => 2
                [parent_id]    => 1
                [parent_count] => 1
                [children]     => array
                (
                    [0] => array
                    (
                        [id]           => 3
                        [parent_id]    => 2
                        [parent_count] => 2
                        [children]     => array
                        (
                            [0] => array
                            (
                                [id]           => 4
                                [parent_id]    => 3
                                [parent_count] => 3,
                            ),
                        ),
                    ),
                ),
            )
            [1] => array
            (
                [id]           => 5
                [parent_count] => 1
                [parent_id]    => 1,
            ),
        ),
    )
    [1] => array
    (
        [id]           => 1
        [parent_id]    => 0
        [parent_count] => 0
        [children]     => array
        (
            [id]           => 2
            [parent_id]    => 1
            [parent_count] => 1
            [children]     => array
            (
                [id]           => 3
                [parent_id]    => 2
                [parent_count] => 2,
            ),
        ),
    )
    [2] => array
    (
        [id]           => 1
        [parent_id]    => 0
        [parent_count] => 0,
    ),
)

这里是原始数组

$data[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'        => 4,
                            'parent_id' => 3,
                        ),
                    ),
                ),
            ),
        ),
        '1' => array(
            'id'        => 5,
            'parent_id' => 1,
        ),
    ),
);
$data[1] = array(
    'id'        => 1,
    'parent_id' => 0,
    'children'  => array(
        'id'        => 2,
        'parent_id' => 1,
        'children'  => array(
            'id'        => 3,
            'parent_id' => 2,
        ),
    ),
);
$data[2] = array(
    'id'        => 1,
    'parent_id' => 0,
);

这是我的递归函数

function recursive(array $data, $count = 0)
{
    $count_parent = array();
    foreach ($data as $key => $dat) {
        if ($dat['parent_id'] == 0) {
            $data['count_parent'] = 0;
            if (isset($dat['children'])) {
                recursive($dat['children'], $count);
            }
        } else if ($dat['parent_id'] != 0 && array_key_exists('children', $dat)) {
            $count++;
            $data[$key]['count_parent'] = $count;
            if (isset($dat['children'])) {
                recursive($dat['children'], $count);
            }
        } else if ($dat['parent_id'] != 0 && !array_key_exists('children', $dat)) {
            $count++;
            $data[$key]['count_parent'] = $count;
        } else {
            $data['count_parent'] = 0;
        }
        $count_parent = $data;
    }
    return $count_parent;
}

它不起作用。我不知道如何创建递归,我是使用这种方法的新手。

我真的很感激一些帮助。

谢谢:)

1 个答案:

答案 0 :(得分:1)

试试这个:

<?php

function recursive(&$data, $count = 0) {
    $current_parent = "";

    foreach ($data as $key => &$dat) {
        if (is_array($dat)) {
            if ($current_parent != $dat['parent_id']) {
                $count++;
                $current_parent = $dat['parent_id'];
            }
            if ($dat['parent_id'] == 0)
                $count = 0;
            $dat['count_parent'] = $count;
        }
        if (is_array($dat) && array_key_exists('children', $dat)) {
            recursive($dat['children'], $count);
        }
    }
}

recursive($data);
echo "<pre>";
print_r($data);