I am trying to get it so that the output that is as follows (currently):
┌── 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
to be correct.
Obviously, the actual outcome i want is where not every square has the vertical lines, if that node has already reached its end of the line └──
.
This is my current code:
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;
}
This is the array to be passed in:
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(),
),
),
),
);