php查詢顯示數(shù)據(jù)庫中的菜單表的數(shù)據(jù),生成二維數(shù)據(jù),我想通過數(shù)組處理,把子菜單作為父菜單的一個鍵值進行組合,哪位幫我實現(xiàn)一下,非常感謝!簡單的說就是下面的數(shù)組重新組合成后面的.
原數(shù)組
Array
(
[0] => Array
(
[id] => 1
[pid] => 0
[name] => /Admin/Index/home
[title] => 管理首頁
[type] => 1
[icon] => clone
[status] => 1
[condition] =>
[sort] => 1
)
[1] => Array
(
[id] => 14
[pid] => 13
[name] => /Admin/Cate/index
[title] => 分類管理
[type] => 1
[icon] => clone
[status] => 1
[condition] =>
[sort] => 1
)
[2] => Array
(
[id] => 3
[pid] => 2
[name] => /Admin/Set/webset
[title] => 系統(tǒng)設置
[type] => 1
[icon] => mobile
[status] => 1
[condition] =>
[sort] => 1
)
[3] => Array
(
[id] => 21
[pid] => 20
[name] => /Admin/Other/comment
[title] => 評論管理
[type] => 1
[icon] => user
[status] => 1
[condition] =>
[sort] => 1
)
[4] => Array
(
[id] => 15
[pid] => 13
[name] => /Admin/Cate/add
[title] => 分類添加
[type] => 1
[icon] => plus
[status] => 1
[condition] =>
[sort] => 2
)
[5] => Array
(
[id] => 22
[pid] => 20
[name] => /Admin/Other/number
[title] => 注冊碼管理
[type] => 1
[icon] => bars
[status] => 1
[condition] =>
[sort] => 2
)
[6] => Array
(
[id] => 4
[pid] => 2
[name] => /Admin/Set/user
[title] => 用戶管理
[type] => 1
[icon] => users
[status] => 1
[condition] =>
[sort] => 2
)
[7] => Array
(
[id] => 2
[pid] => 0
[name] =>
[title] => 網(wǎng)站設置
[type] => 1
[icon] => shopping-cart
[status] => 1
[condition] =>
[sort] => 2
)
[8] => Array
(
[id] => 23
[pid] => 20
[name] => /Admin/Other/gbook
[title] => 留言管理
[type] => 1
[icon] => send-o
[status] => 1
[condition] =>
[sort] => 3
)
[9] => Array
(
[id] => 16
[pid] => 13
[name] => /Admin/Info/index
[title] => 信息管理
[type] => 1
[icon] => check-square
[status] => 1
[condition] =>
[sort] => 3
)
[10] => Array
(
[id] => 13
[pid] => 0
[name] =>
[title] => 內容管理
[type] => 1
[icon] => comments-o
[status] => 1
[condition] =>
[sort] => 3
)
[11] => Array
(
[id] => 5
[pid] => 2
[name] => /Admin/Set/email
[title] => 郵件設置
[type] => 1
[icon] => weixin
[status] => 1
[condition] =>
[sort] => 3
)
[12] => Array
(
[id] => 6
[pid] => 2
[name] => /Admin/Set/mysql
[title] => 數(shù)據(jù)庫設置
[type] => 1
[icon] => thumbs-up
[status] => 1
[condition] =>
[sort] => 4
)
[13] => Array
(
[id] => 20
[pid] => 0
[name] =>
[title] => 擴展功能
[type] => 1
[icon] => cubes
[status] => 1
[condition] =>
[sort] => 4
)
[14] => Array
(
[id] => 17
[pid] => 13
[name] => /Admin/Info/infoadd
[title] => 信息添加
[type] => 1
[icon] => users
[status] => 1
[condition] =>
[sort] => 4
)
[15] => Array
(
[id] => 24
[pid] => 20
[name] => /Admin/Tpl/index
[title] => 模板管理
[type] => 1
[icon] => users
[status] => 1
[condition] =>
[sort] => 4
)
[16] => Array
(
[id] => 25
[pid] => 20
[name] => /Admin/Other/linkindex
[title] => 鏈接管理
[type] => 1
[icon] => weixin
[status] => 1
[condition] =>
[sort] => 5
)
[17] => Array
(
[id] => 18
[pid] => 13
[name] => /Admin/Info/page
[title] => 單頁管理
[type] => 1
[icon] => cubes
[status] => 1
[condition] =>
[sort] => 5
)
[18] => Array
(
[id] => 29
[pid] => 0
[name] => /Admin/Index/delcache
[title] => 刪除緩存
[type] => 1
[icon] => cog
[status] => 1
[condition] =>
[sort] => 5
)
[19] => Array
(
[id] => 7
[pid] => 2
[name] => /Admin/Data/backupform
[title] => 數(shù)據(jù)庫備份
[type] => 1
[icon] => cog
[status] => 1
[condition] =>
[sort] => 5
)
[20] => Array
(
[id] => 26
[pid] => 20
[name] => /Admin/Other/linkadd
[title] => 鏈接添加
[type] => 1
[icon] => users
[status] => 1
[condition] =>
[sort] => 6
)
[21] => Array
(
[id] => 30
[pid] => 0
[name] => /Admin/Lognn/logout
[title] => 退出登錄
[type] => 1
[icon] => refresh
[status] => 1
[condition] =>
[sort] => 6
)
[22] => Array
(
[id] => 19
[pid] => 13
[name] => /Admin/Info/pageadd
[title] => 單頁添加
[type] => 1
[icon] => book
[status] => 1
[condition] =>
[sort] => 6
)
[23] => Array
(
[id] => 8
[pid] => 2
[name] => /Admin/Set/url
[title] => URL模式設置
[type] => 1
[icon] => refresh
[status] => 1
[condition] =>
[sort] => 6
)
[24] => Array
(
[id] => 9
[pid] => 2
[name] => /Admin/Set/theme
[title] => 主題設置
[type] => 1
[icon] => bars
[status] => 1
[condition] =>
[sort] => 7
)
[25] => Array
(
[id] => 27
[pid] => 20
[name] => /Admin/Notice/index
[title] => 廣告管理
[type] => 1
[icon] => weixin
[status] => 1
[condition] =>
[sort] => 7
)
[26] => Array
(
[id] => 10
[pid] => 2
[name] => /Admin/Set/image
[title] => 圖片上傳設置
[type] => 1
[icon] => th
[status] => 1
[condition] =>
[sort] => 8
)
[27] => Array
(
[id] => 28
[pid] => 20
[name] => /Admin/Index/sitemap
[title] => sitemap生成
[type] => 1
[icon] => thumbs-up
[status] => 1
[condition] =>
[sort] => 8
)
[28] => Array
(
[id] => 11
[pid] => 2
[name] => /Admin/Set/path
[title] => 相關路徑設置
[type] => 1
[icon] => check-square
[status] => 1
[condition] =>
[sort] => 9
)
[29] => Array
(
[id] => 12
[pid] => 2
[name] => /Admin/Amenu/index
[title] => 后臺菜單管理
[type] => 1
[icon] => recycle
[status] => 1
[condition] =>
[sort] => 10
)
)
Array
(
[0] => Array
(
[id] => 1
[pid] => 0
[name] => /Admin/Index/home
[title] => 管理首頁
[type] => 1
[icon] => clone
[status] => 1
[condition] =>
[sort] => 1
)
[1] => Array
(
[id] => 2
[pid] => 0
[name] =>
[title] => 網(wǎng)站設置
[type] => 1
[icon] => shopping-cart
[status] => 1
[condition] =>
[sort] => 2
[submenu] => Array
(
[0] => Array
(
[id] => 3
[pid] => 2
[name] => /Admin/Set/webset
[title] => 系統(tǒng)設置
[type] => 1
[icon] => mobile
[status] => 1
[condition] =>
[sort] => 1
)
[1] => Array
(
[id] => 4
[pid] => 2
[name] => /Admin/Set/user
[title] => 用戶管理
[type] => 1
[icon] => users
[status] => 1
[condition] =>
[sort] => 2
)
[2] => Array
(
[id] => 5
[pid] => 2
[name] => /Admin/Set/email
[title] => 郵件設置
[type] => 1
[icon] => weixin
[status] => 1
[condition] =>
[sort] => 3
)
[3] => Array
(
[id] => 6
[pid] => 2
[name] => /Admin/Set/mysql
[title] => 數(shù)據(jù)庫設置
[type] => 1
[icon] => thumbs-up
[status] => 1
[condition] =>
[sort] => 4
)
[4] => Array
(
[id] => 7
[pid] => 2
[name] => /Admin/Data/backupform
[title] => 數(shù)據(jù)庫備份
[type] => 1
[icon] => cog
[status] => 1
[condition] =>
[sort] => 5
)
[5] => Array
(
[id] => 8
[pid] => 2
[name] => /Admin/Set/url
[title] => URL模式設置
[type] => 1
[icon] => refresh
[status] => 1
[condition] =>
[sort] => 6
)
[6] => Array
(
[id] => 9
[pid] => 2
[name] => /Admin/Set/theme
[title] => 主題設置
[type] => 1
[icon] => bars
[status] => 1
[condition] =>
[sort] => 7
)
[7] => Array
(
[id] => 10
[pid] => 2
[name] => /Admin/Set/image
[title] => 圖片上傳設置
[type] => 1
[icon] => th
[status] => 1
[condition] =>
[sort] => 8
)
[8] => Array
(
[id] => 11
[pid] => 2
[name] => /Admin/Set/path
[title] => 相關路徑設置
[type] => 1
[icon] => check-square
[status] => 1
[condition] =>
[sort] => 9
)
[9] => Array
(
[id] => 12
[pid] => 2
[name] => /Admin/Amenu/index
[title] => 后臺菜單管理
[type] => 1
[icon] => recycle
[status] => 1
[condition] =>
[sort] => 10
)
)
)
[2] => Array
(
[id] => 13
[pid] => 0
[name] =>
[title] => 內容管理
[type] => 1
[icon] => comments-o
[status] => 1
[condition] =>
[sort] => 3
[submenu] => Array
(
[0] => Array
(
[id] => 14
[pid] => 13
[name] => /Admin/Cate/index
[title] => 分類管理
[type] => 1
[icon] => clone
[status] => 1
[condition] =>
[sort] => 1
)
[1] => Array
(
[id] => 15
[pid] => 13
[name] => /Admin/Cate/add
[title] => 分類添加
[type] => 1
[icon] => plus
[status] => 1
[condition] =>
[sort] => 2
)
[2] => Array
(
[id] => 16
[pid] => 13
[name] => /Admin/Info/index
[title] => 信息管理
[type] => 1
[icon] => check-square
[status] => 1
[condition] =>
[sort] => 3
)
[3] => Array
(
[id] => 17
[pid] => 13
[name] => /Admin/Info/infoadd
[title] => 信息添加
[type] => 1
[icon] => users
[status] => 1
[condition] =>
[sort] => 4
)
[4] => Array
(
[id] => 18
[pid] => 13
[name] => /Admin/Info/page
[title] => 單頁管理
[type] => 1
[icon] => cubes
[status] => 1
[condition] =>
[sort] => 5
)
[5] => Array
(
[id] => 19
[pid] => 13
[name] => /Admin/Info/pageadd
[title] => 單頁添加
[type] => 1
[icon] => book
[status] => 1
[condition] =>
[sort] => 6
)
)
)
[3] => Array
(
[id] => 20
[pid] => 0
[name] =>
[title] => 擴展功能
[type] => 1
[icon] => cubes
[status] => 1
[condition] =>
[sort] => 4
[submenu] => Array
(
[0] => Array
(
[id] => 21
[pid] => 20
[name] => /Admin/Other/comment
[title] => 評論管理
[type] => 1
[icon] => user
[status] => 1
[condition] =>
[sort] => 1
)
[1] => Array
(
[id] => 22
[pid] => 20
[name] => /Admin/Other/number
[title] => 注冊碼管理
[type] => 1
[icon] => bars
[status] => 1
[condition] =>
[sort] => 2
)
[2] => Array
(
[id] => 23
[pid] => 20
[name] => /Admin/Other/gbook
[title] => 留言管理
[type] => 1
[icon] => send-o
[status] => 1
[condition] =>
[sort] => 3
)
[3] => Array
(
[id] => 24
[pid] => 20
[name] => /Admin/Tpl/index
[title] => 模板管理
[type] => 1
[icon] => users
[status] => 1
[condition] =>
[sort] => 4
)
[4] => Array
(
[id] => 25
[pid] => 20
[name] => /Admin/Other/linkindex
[title] => 鏈接管理
[type] => 1
[icon] => weixin
[status] => 1
[condition] =>
[sort] => 5
)
[5] => Array
(
[id] => 26
[pid] => 20
[name] => /Admin/Other/linkadd
[title] => 鏈接添加
[type] => 1
[icon] => users
[status] => 1
[condition] =>
[sort] => 6
)
[6] => Array
(
[id] => 27
[pid] => 20
[name] => /Admin/Notice/index
[title] => 廣告管理
[type] => 1
[icon] => weixin
[status] => 1
[condition] =>
[sort] => 7
)
[7] => Array
(
[id] => 28
[pid] => 20
[name] => /Admin/Index/sitemap
[title] => sitemap生成
[type] => 1
[icon] => thumbs-up
[status] => 1
[condition] =>
[sort] => 8
)
)
)
[4] => Array
(
[id] => 29
[pid] => 0
[name] => /Admin/Index/delcache
[title] => 刪除緩存
[type] => 1
[icon] => cog
[status] => 1
[condition] =>
[sort] => 5
)
[5] => Array
(
[id] => 30
[pid] => 0
[name] => /Admin/Lognn/logout
[title] => 退出登錄
[type] => 1
[icon] => refresh
[status] => 1
[condition] =>
[sort] => 6
)
)
閉關修行中......
$data = array(
array('id' => 1, 'pid' => 0),
array('id' => 2, 'pid' => 0),
array('id' => 3, 'pid' => 1),
array('id' => 4, 'pid' => 1),
array('id' => 5, 'pid' => 3),
);
$tree = [];
foreach($data as $v){
$tree[$v['id']] = $v;
$tree[$v['id']]['submenu'] = array();
}
foreach ($tree as $k=>$item) {
if ($item['pid'] != 0) {
$tree[$item['pid']]['submenu'][] = &$tree[$k];
}
}
print_r($tree);
//輸出
Array
(
[1] => Array
(
[id] => 1
[pid] => 0
[submenu] => Array
(
[0] => Array
(
[id] => 3
[pid] => 1
[submenu] => Array
(
[0] => Array
(
[id] => 5
[pid] => 3
[submenu] => Array
(
)
)
)
)
[1] => Array
(
[id] => 4
[pid] => 1
[submenu] => Array
(
)
)
)
)
[2] => Array
(
[id] => 2
[pid] => 0
[submenu] => Array
(
)
)
[3] => Array
(
[id] => 3
[pid] => 1
[submenu] => Array
(
[0] => Array
(
[id] => 5
[pid] => 3
[submenu] => Array
(
)
)
)
)
[4] => Array
(
[id] => 4
[pid] => 1
[submenu] => Array
(
)
)
[5] => Array
(
[id] => 5
[pid] => 3
[submenu] => Array
(
)
)
)
源數(shù)據(jù)不好,應該order by pid, id,這樣會產(chǎn)生類似以下數(shù)據(jù):
$data = array(
array('id' => 1, 'pid' => 0),
array('id' => 2, 'pid' => 0),
array('id' => 3, 'pid' => 1),
array('id' => 4, 'pid' => 1),
array('id' => 5, 'pid' => 3),
);
然后通過一次for循環(huán)將下標設為id值(有的框架搜索結果已經(jīng)用了這種形式的可以省略這一步)
$tree = [];
foreach ($data as $row) {
$tree[$row['id']] = $row;
}
然后構造樹形結構
foreach ($tree as $id => $row) {
if (! isset($row['_childs'])) $tree[$id]['_childs'] = [];
if (! empty($row['pid'])) {
$tree[$row['pid']]['_childs'][] = &$tree[$id];
}
}
//現(xiàn)在包含了冗余數(shù)據(jù)
print_r($tree);
可以去掉冗余部分,也可以留著
foreach ($tree as $id => $row) {
if (! empty($row['pid'])) unset($tree[$id]);
}
print_r($tree);