我坚持使用树生成算法:
数组示例:
$start = 1;
$depth = 3;
$s = array(
'name' => 1, // taken from $start
'children' => array(
array(
'name' => 2,
'children => array(
array( 'name' => 3 ), /* No children for last node $depth */
array( 'name' => 3 ), /* No children for last node $depth */
)
),
array(
'name' => 2,
'children => array(
// same as above
)
),
)
);
此时我已经提出了一个非常难看的功能,并希望得到任何帮助或建议来构建更好的算法。
答案 0 :(得分:1)
这应该是有帮助的
function generateTree($depth, $level = 0)
{
$result = array();
if ($depth == $level) {
$result = array('name' => $level);
} else {
$result = array('name' => $level, 'children' => array(generateTree($depth, $level + 1), generateTree($depth, $level + 1)));
}
return $result;
}
print_r(generateTree(3, 1));