开发者

Get all the latest children

开发者 https://www.devze.com 2023-04-02 07:24 出处:网络
I need a little help. My knowledge of algorithms are开发者_如何学编程 weak. I can not write a recursive function in PHP that returns all the latest children.

I need a little help. My knowledge of algorithms are开发者_如何学编程 weak. I can not write a recursive function in PHP that returns all the latest children.

Suppose our array looks like this:

Array
(
[0] => Array
    (
        [id_category] => 1
        [name] => Accueil
        [id_parent] => 0
    )

[1] => Array
    (
        [id_category] => 2
        [name] => Accessoires
        [id_parent] => 1
    )

[2] => Array
    (
        [id_category] => 3
        [name] => Merchandising
        [id_parent] => 1
    )

[3] => Array
    (
        [id_category] => 4
        [name] => Pièces détachées
        [id_parent] => 1
    )

[4] => Array
    (
        [id_category] => 5
        [name] => Excavateur
        [id_parent] => 4
    )

[5] => Array
    (
        [id_category] => 6
        [name] => série 100
        [id_parent] => 5
    )

[6] => Array
    (
        [id_category] => 7
        [name] => above
        [id_parent] => 6
    )

[7] => Array
    (
        [id_category] => 8
        [name] => système hydraulique
        [id_parent] => 7
    )

[8] => Array
    (
        [id_category] => 9
        [name] => série 200
        [id_parent] => 5
    )

[9] => Array
    (
        [id_category] => 10
        [name] => thru
        [id_parent] => 6
    )

[10] => Array
    (
        [id_category] => 11
        [name] => Compaction
        [id_parent] => 4
    )

[11] => Array
    (
        [id_category] => 12
        [name] => système électrique
        [id_parent] => 7
    )

)

I would like getLastChildren (5) or getLastChildren (6) or getLastChildren (7), the function returns me an answer array ("8", "12")

I will try to give an example.

If I take the category 5: = 6 and 9 are the children.

I look through children. The child 6 has two children, 7 and 10, Child 9: no children.

I put 9 in the list of children.

The child 7 has two children, 8 and 12. 8 has no children. 12 has no children. I add 8 and 12.

So we return (9,8,12) 10 has no children. I also added.

In the end I (9,8,12,10)

So what I would do, if I search "all the last children in category 7" => 8, and 12. I hope my explanation is "a little clearer."


I would do something like that:

$array = array(
    array (
        'id_category' => 1,
        'name' => 'Accueil',
        'id_parent' => 0,
    ),
    array (
        'id_category' => 2,
        'name' => 'Accessoires',
        'id_parent' => 1,
    ),
    array (
        'id_category' => 3,
        'name' => 'Merchandising',
        'id_parent' => 1,
    ),
    array (
        'id_category' => 4,
        'name' => 'Pièces détachées',
        'id_parent' => 1,
    ),
    array (
        'id_category' => 5,
        'name' => 'Excavateur',
        'id_parent' => 4,
    ),
    array (
        'id_category' => 6,
        'name' => 'série 100',
        'id_parent' => 5,
    ),
    array (
        'id_category' => 7,
        'name' => 'above',
        'id_parent' => 6,
    ),
    array (
        'id_category' => 8,
        'name' => 'système hydraulique',
        'id_parent' => 7,
    ),
    array (
        'id_category' => 9,
        'name' => 'série 200',
        'id_parent' => 5,
    ),
    array (
        'id_category' => 10,
        'name' => 'thru',
        'id_parent' => 6,
    ),
    array (
        'id_category' => 11,
        'name' => 'Compaction',
        'id_parent' => 4,
    ),
    array (
        'id_category' => 12,
        'name' => 'système électrique',
        'id_parent' => 7,
    ),
);

I split data and code, here is the code:

$children = array();
function getLastChildren($array, $parent) {
global $children;
    foreach ($array as $key => $value) {
        if ($value['id_parent'] == $parent) {
            if (hasChild($array, $value['id_category'])) {
                getLastChildren($array, $value['id_category']);
            } else {
                $children[] = $value['id_category'];
            }
        }
    }
}

function hasChild($array, $parent) {
    foreach ($array as $key => $value) {
        if ($value['id_parent'] == $parent) {
            return true;
        }
    }
    return false;
}

getLastChildren($array, 5);
print_r($children);

output:

Array
(
    [0] => 8
    [1] => 12
    [2] => 10
    [3] => 9
)
0

精彩评论

暂无评论...
验证码 换一张
取 消