我试图得到它,以便输出如下(当前):
┌── Level 0 Child 1
└── Level 0 Child 2
| ├── Level 1 Child 1
| | └── Level 1 Child 1 Grandchild 1
| └── Level 1 Child 2
| | ├── Level 1 Child 2 Grandchild 1
| | | ├── Level 1 Child 2 Grandchild 1 Great-grandchild 1
| | | ├── Level 1 Child 2 Grandchild 1 Great-grandchild 2
| | | └── Level 1 Child 2 Grandchild 1 Great-grandchild 3
| | └── Level 1 Child 2 Grandchild 2
| | | └── Level 1 Child 2 Grandchild 2 Great-grandchild 1
是正确的。
显然,我想要的实际结果是,如果该节点已经到达行└──
的末尾,则不是每个正方形都有垂直线。
这是我目前的代码:
function makeList(array $array, $level = 0, $very_first_item = true)
{
$output = "\n";
$count = 0;
foreach ($array as $key => $subArray) {
if($count == count($array) - 1) {
$prefix = str_repeat('| ', $level) . '└── ';
} elseif($very_first_item === true) {
$prefix = str_repeat('| ', $level) . '┌── ';
} else {
$prefix = str_repeat('| ', $level) . '├── ';
}
$very_first_item = false;
$output .= $prefix . $key . makeList($subArray, $level + 1, $very_first_item);
$count++;
}
return $output;
}
这是要传入的数组:
array(
'Level 0 Child 1' =>
array(),
'Level 0 Child 2' =>
array(
'Level 1 Child 1' =>
array(
'Level 1 Child 1 Grandchild 1' =>
array(),
),
'Level 1 Child 2' =>
array(
'Level 1 Child 2 Grandchild 1' =>
array(
'Level 1 Child 2 Grandchild 1 Great-grandchild 1' =>
array(),
'Level 1 Child 2 Grandchild 1 Great-grandchild 2' =>
array(),
'Level 1 Child 2 Grandchild 1 Great-grandchild 3' =>
array(),
),
'Level 1 Child 2 Grandchild 2' =>
array(
'Level 1 Child 2 Grandchild 2 Great-grandchild 1' =>
array(),
),
),
),
);
答案 0 :(得分:0)
请检查此代码是否可以为您提供帮助。
我更改了您的makeList
函数,还添加了另一个名为array_depth
的函数,该函数使用this post计算数组的深度,并在您的末尾添加了另一个节点用于检查代码是否正常工作的数组。所以,这是代码:
$myArr = array(
'Level 0 Child 1' => array(),
'Level 0 Child 2' => array(
'Level 1 Child 1' => array(
'Level 1 Child 1 Grandchild 1' => array(),
),
'Level 1 Child 2' => array(
'Level 1 Child 2 Grandchild 1' => array(
'Level 1 Child 2 Grandchild 1 Great-grandchild 1' => array(),
'Level 1 Child 2 Grandchild 1 Great-grandchild 2' => array(),
'Level 1 Child 2 Grandchild 1 Great-grandchild 3' => array(),
),
'Level 1 Child 2 Grandchild 2' => array(
'Level 1 Child 2 Grandchild 2 Great-grandchild 1' => array(),
),
),
),
'Level 0 Child 3' => array(
'Level 1 Child 1' => array(
'Level 1 Child 1 Grandchild 1' => array(),
),
)
);
function array_depth(array $array){
$max_depth = 1;
foreach ($array as $value){
if (is_array($value)){
$depth = array_depth($value) + 1;
if ($depth > $max_depth){
$max_depth = $depth;
}
}
}
return $max_depth;
}
function makeList(array $array, $level = 0, $very_first_item = true)
{
global $arrLines;
$output = "\n";
$count = 0;
foreach ($array as $key => $subArray) {
if($count == count($array) - 1) {
$prefix = '└── ';
$arrLines[$level + 1] = ' ';
} elseif($very_first_item === true) {
$prefix = '┌── ';
$arrLines[$level + 1] = '| ';
} else {
$prefix = '├── ';
$arrLines[$level + 1] = '| ';
}
$very_first_item = false;
for($iCnt = 1; $iCnt <= $level; $iCnt++)
$output .= $arrLines[$iCnt];
$output .= $prefix . $key . makeList($subArray, $level + 1, $very_first_item);
$count++;
}
return $output;
}
$arrLines = array(array_depth($myArr));
echo '<pre>'.makeList($myArr).'</pre>';