Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
$tree = [ 0 => [ [ 'name' => 'Уровень 1.1', 'id' => 2, 'pid' => 1 ],
[ 'name' => 'Уровень 1.2', 'id' => 3, 'pid' => 1 ],
[ 'name' => 'Уровень 1.3', 'id' => 4, 'pid' => 1 ],
[ 'name' => 'Уровень 1.4', 'id' => 12, 'pid' => 1, 'chld' => [ [ 'name' => 'Уровень 1.4.1', 'id' => 15, 'pid' => 12 ]
]
]
]
];
function printTree($tree) {
$stack = new SplStack();
$stack->push($tree);
while (!$stack->isEmpty()) {
$node = $stack->pop();
echo str_repeat('-', $node['level']) . $node['name'] . "\n";
if (isset($node['chld'])) {
foreach (array_reverse($node['chld']) as $child) {
$child['level'] = $node['level'] + 1;
$stack->push($child);
}
}
}
}
$tree['level'] = 2;
printTree($tree);