Предположим, есть задачка, вывести дерево. Под рукой PHP, а как view — Twig. Пусть структура данных на backend будет такой: $tree = [ [ ‘name’ => ‘foo’, ‘children’ => [ [ ‘name’ => ‘bar’, ‘children’ => [ [ ‘name’ => ‘baz’, ‘children’ => [ ], ], [ ‘name’ => ‘baz’, ‘children’ => [ ], ],
Browsing tag: tree
13.02.2018
23.06.2016
Requirements = PHP 5.3 function buildTree($flat, $pidKey, $idKey = null) { $grouped = array(); foreach ($flat as $sub){ $grouped[$sub[$pidKey]][] = $sub; } $fnBuilder = function($siblings) use (&$fnBuilder, $grouped, $idKey) { foreach ($siblings as $k => $sibling) { $id = $sibling[$idKey]; if(isset($grouped[$id])) { $sibling[‘children’] = $fnBuilder($grouped[$id]); } $siblings[$k] = $sibling; } return $siblings; }; $tree =