问题描述
我想通过多维数组找到最大路径。它被设置为一个树形结构。
编辑:我想要的是如何找到数组的最大跨度。
$ 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'],
],
],
],
],
],
],
],
];
我想要做的就是把它加入到一个函数中,然后返回
1。每一种独特的路径如
45,3,88 = 136
45,2,77 = 124
45 ,5,67,2,35 = 154
45,5,67,3,44 = 164
2。或者最大路径,就是最高的路径。
164
我从一些漂亮的随机数据,所以它们有时候是10s或数百个层次,100s或1000s的独特路径。
我相信您可以更高效地执行此项任务。
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.
I believe you can perform this task more efficiently using array_walk_recursive.
这篇关于遍历一个像数组一样的树来找到PHP中的最大跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!