Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to find the max path through a multidimensional array. It is set up like a tree structure.

EDIT: I guess what I am looking for is how to find a Maximum Spanning for an array.

$tree = [ 
    0 => '45', 
    1 => [ 
        0 => [ 
            0 => '3', 
            1 => [ 
                    0 => [0 => '88'],
                ], 
            ], 
        1 => [ 
            0 => '2', 
            1 => [ 
                    0 => [ 0 => '77'], 
                ], 
            ],
        2 => [ 
            0 => '5', 
            1 => [ 
                0 => [ 
                    0 => '67', 
                    1 => [ 
                        0 => [ 
                            0 => '2', 
                            1 => [ 
                                0 => [ 0 => '35' ], 
                                ], 
                            ], 
                        1 => [ 
                            0 => '3', 
                            1 => [ 
                                0 => [ 0 => '44' ], 
                                ], 
                            ], 
                        ], 
                    ], 
                ], 
            ], 
        ], 
    ];

What I want to do is feed this into a function and get back

1. Every unique set of paths like

45, 3, 88 = 136
45, 2, 77 = 124
45, 5, 67, 2, 35 = 154
45, 5, 67, 3, 44 = 164

2. Or the max path, just the highest of those.

164

I generate these trees from some pretty random data so they are sometimes 10s or hundreds of tiers and 100s or 1000s of unique paths.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
124 views
Welcome To Ask or Share your Answers For Others

1 Answer

I believe you can perform this task more efficiently using array_walk_recursive.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...