<?php
$tree = [
'level1' => [
'val1',
'val2',
'val3',
'level2' => [
'val4',
'val5',
],
],
];
$iterator = new RecursiveTreeIterator(
new RecursiveArrayIterator($tree)
);
$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_LEFT, ' ');
$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_MID_HAS_NEXT, '');
$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_MID_LAST, '|');
$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_END_HAS_NEXT, '-');
$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_END_LAST, '\\');
$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_RIGHT, ' ');
echo '<pre>';
foreach ($iterator as $val) {
echo $val . PHP_EOL;
}
echo '</pre>';
\ Array |- val1 |- val2 |- val3 |\ Array ||- val4 ||\ val5