It will seem to someone that the task is sucked, but I didn’t find a sane solution, so please help me what you can :-)
So, there is a standard table category (id, title, parent_id). Parent_id is a foreign key that refers to the id of the parent of the same table.
I pull out all categories from the base with one query:
$categories = Category::find()->asArray()->all(); The $ categories array is of the form:
[ ['id'=>0, 'title'=>'Электроника', 'parent_id' => null], ['id'=>1, 'title'=>'Компьютеры', 'parent_id' => 0], ['id'=>2, 'title'=>'ПК', 'parent_id' => 1], ['id'=>3, 'title'=>'Ноутбуки', 'parent_id' => 1], ['id'=>4, 'title'=>'Мобильные телефоны', 'parent_id' => 0], ['id'=>5, 'title'=>'Бытовая химия', 'parent_id' => null], ['id'=>6, 'title'=>'Порошок', 'parent_id' => 5], ['id'=>7, 'title'=>'Мыло', 'parent_id' => 5], ] etc., categories of infinite (unknown) nesting.
It is necessary to make another multidimensional array-tree from this array, in which the child categories are nested in the parent ones (for the subsequent tree-like output in the views). How to do it? What are the options? Thank.