php - 无法理解嵌套多次数组的逻辑
问题描述
我在函数中的逻辑有问题。我正在尝试将数组嵌套 X 次,以便它看起来像一棵有孩子的树。我将它与第一个孩子嵌套,但是当我深入时,我找不到删除它们的解决方案从主数组中并将它们添加到深处。“父”键是需要有子元素的父元素的 ID。任何帮助表示赞赏。
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 '</pre>';
}
现在我收到这样的数组
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
)
)
解决方案
在 get_all 函数的第一个 foreach 中,您每次都将 $full_menu arrach 的元素添加到 $mega_menu,如果 $fm->parent != 0 则事件(这意味着每个子元素都保存到数组的第一级)。接下来你处理孩子。您必须仅保存(在数组的第一维)具有 $fm->parent == 0 的那些元素。您可以通过更改 get_all 中的条件来做到这一点。
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) {
if($fm->parent == 0) {
$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
);
} else {
$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 '</pre>';
}
推荐阅读
- javascript - 如何在服务器端禁用按钮提交几秒钟
- jdbc - Why does jdbc DriverManager getConnection() need synchronized(DriverManager.class)?
- java - Kotlin 视图 setOnTouchListener 未触发(我参考了所有文档)
- blockchain - 矿工敲诈勒索导致去中心化应用程序的局限性?
- swift - 如何将 Swift 字符串与 NSLineSeparatorCharacter 常量连接起来
- javascript - Javascript检查JSON数组中是否存在数组所有项目
- swiftui - SwiftUI Picker and NavigationLink Gesture Conflicts
- sql - 过滤 PIVOT 信息
- selenium - Selenium - 上传多个文件会导致上传重复文件(第一个文件一次又一次)
- reactjs - React 站点 - 刷新时,Auth Route 无权访问 auth 上下文数据