Browsing tag: nested-list

Flat array to nested in php


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 =

Яндекс.Метрика