2

How to count parent array using recursive..

Here's my array

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,
    ),

)

the result what I want something like this..

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,
    ),
)

Here the raw array

$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,
);

Here my recursive function

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;
}

It didn't work. I don't know how to create recursive, I'm new using this method.

I would really appreciate some help.

Thanks :)

6
  • 2
    Please format it properly and scale down the array size if possible. Commented Feb 21, 2017 at 10:59
  • You are calling your function recursively but you are not doing anything with the values it returns. Commented Feb 21, 2017 at 11:01
  • Do you need something like a "current depth" counter? Commented Feb 21, 2017 at 11:02
  • In your example parent_id & parent_count are same. Is it for all scenario? Commented Feb 21, 2017 at 11:14
  • @apokryfos ya, something like that, but i want every child count their all parents and adding new key total their parents, sorry for my bad english.. Commented Feb 21, 2017 at 12:37

1 Answer 1

2

Try this:

<?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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.