У меня проблема с моей логикой в функции. Я пытаюсь вложить массив X раз, чтобы он мог выглядеть как дерево с детьми. Я вложил его в первый дочерний элемент, но когда я углубляюсь, я не могу найти решениеудалить их из массива Main и добавить их в глубину. Ключ «parent» - это идентификатор родительского элемента, который должен иметь child.Any помощь оцените.
<code>function array_search_multidim($array, $column, $key){
return (array_search($key, array_column($array, $column)));
}
public function get_all() {
$full_menu = $this->Site_model->get_all_menu();
usort($full_menu, function($a, $b){
return strcmp($a->menu_order, $b->menu_order);
});
foreach($full_menu as $fm) {
$mega_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
if($fm->parent != 0) {
$child_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
}
}
foreach($child_menu as $cm) {
$mega_menu[$this->array_search_multidim($mega_menu,'id',$cm['parent'])]['children'][] = array(
'id' => $cm['id'],
'text' => $cm['text'],
'href' => $cm['href'],
'icon' => $cm['icon'],
'target' => $cm['target'],
'title' => $cm['title'],
'order' => $cm['order'],
'parent' => $cm['parent']
);
}
echo '<pre>';
print_r($mega_menu);
echo '
';}
А сейчас я получаю такой массив
Array
(
[0] => Array
(
[id] => 1
[text] => Начало
[href] => http://localhost/roni/#top
[icon] => fas fa-home
[target] => _self
[title] => Начало
[order] => 1
[parent] => 0
)
[1] => Array
(
[id] => 4
[text] => Споделен хостинг
[href] => http://localhost/roni/#shared
[icon] => fas fa-home
[target] => _blank
[title] => shared
[order] => 1
[parent] => 3
[children] => Array
(
[0] => Array
(
[id] => 5
[text] => Софтуер
[href] => http://localhost/roni/#software
[icon] => fas fa-code
[target] => _self
[title] => software
[order] => 2
[parent] => 4
)
)
)
[2] => Array
(
[id] => 2
[text] => Интернет
[href] => http://localhost/roni/#internet2
[icon] => fas fa-wifi
[target] => _top
[title] => Интернет
[order] => 2
[parent] => 0
)
[3] => Array
(
[id] => 5
[text] => Софтуер
[href] => http://localhost/roni/#software
[icon] => fas fa-code
[target] => _self
[title] => software
[order] => 2
[parent] => 4
)
[4] => Array
(
[id] => 3
[text] => Хостинг
[href] => http://localhost/roni/#hosting
[icon] => fas fa-home
[target] => _self
[title] => hosting
[order] => 3
[parent] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[text] => Споделен хостинг
[href] => http://localhost/roni/#shared
[icon] => fas fa-home
[target] => _blank
[title] => shared
[order] => 1
[parent] => 3
)
)
)
[5] => Array
(
[id] => 6
[text] => Сервиз
[href] => http://localhost/roni/#service
[icon] => fas fa-wrench
[target] => _self
[title] => service
[order] => 5
[parent] => 0
)
[6] => Array
(
[id] => 7
[text] => Контакти
[href] => http://localhost/#contacts
[icon] => fas fa-address-book
[target] => _self
[title] => contacts
[order] => 6
[parent] => 0
)
)