PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $tree = [ 'name' => 'Уровень 1', 'id' => 1, 'pid' => 0, 'chld' => [ [ '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);
Show:  
Copy Clear