The problem lies in the correctness of compiling an array of categories and sub categories. Actually the code itself:
The method of creating the tree itself from the array:
public static function getTree($dataset){ $tree = []; foreach ($dataset as $id => &$node) { if (!$node['parent_id']) $tree[$id] = &$node; else $dataset[$node['parent_id']]['childs'][$node['id']] = &$node; } return $tree; } The method in which the created tree is used, and, accordingly, the result is displayed:
public static function getCategoriesList() { $db = Db::getConnection(); $result = $db->query('SELECT id, name, parent_id FROM category WHERE status = "1" ORDER BY sort_order, name ASC'); $i = 0; $categoryList = []; while ($row = $result->fetch()) { $categoryList[$i]['id'] = $row['id']; $categoryList[$i]['name'] = $row['name']; $categoryList[$i]['parent_id'] = $row['parent_id']; $i++; } $tree = Category::getTree($categoryList); echo '<pre>'.print_r($tree, true).'</pre>'; return true; } Category table
And the tree, which turns out:


status = 1? - teran pm