Maison >développement back-end >tutoriel php >求教二维数组转多维数组的函数
php查询显示数据库中的菜单表的数据,生成二维数据,我想通过数组处理,把子菜单作为父菜单的一个键值进行组合,哪位帮我实现一下,非常感谢!简单的说就是下面的数组重新组合成后面的.
原数组
<code> 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] => 系统设置 [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] => 网站设置 [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] => 数据库设置 [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] => 数据库备份 [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 ) )</code>
<code> 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] => 网站设置 [type] => 1 [icon] => shopping-cart [status] => 1 [condition] => [sort] => 2 [submenu] => Array ( [0] => Array ( [id] => 3 [pid] => 2 [name] => /Admin/Set/webset [title] => 系统设置 [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] => 数据库设置 [type] => 1 [icon] => thumbs-up [status] => 1 [condition] => [sort] => 4 ) [4] => Array ( [id] => 7 [pid] => 2 [name] => /Admin/Data/backupform [title] => 数据库备份 [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 ) )</code>
php查询显示数据库中的菜单表的数据,生成二维数据,我想通过数组处理,把子菜单作为父菜单的一个键值进行组合,哪位帮我实现一下,非常感谢!简单的说就是下面的数组重新组合成后面的.
原数组
<code> 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] => 系统设置 [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] => 网站设置 [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] => 数据库设置 [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] => 数据库备份 [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 ) )</code>
<code> 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] => 网站设置 [type] => 1 [icon] => shopping-cart [status] => 1 [condition] => [sort] => 2 [submenu] => Array ( [0] => Array ( [id] => 3 [pid] => 2 [name] => /Admin/Set/webset [title] => 系统设置 [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] => 数据库设置 [type] => 1 [icon] => thumbs-up [status] => 1 [condition] => [sort] => 4 ) [4] => Array ( [id] => 7 [pid] => 2 [name] => /Admin/Data/backupform [title] => 数据库备份 [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 ) )</code>
<code class="php">$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 ( ) ) )</code>
两个循环的事,何必来问
源数据不好,应该order by pid, id,这样会产生类似以下数据:
<code>$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), );</code>
然后通过一次for循环将下标设为id值(有的框架搜索结果已经用了这种形式的可以省略这一步)
<code>$tree = []; foreach ($data as $row) { $tree[$row['id']] = $row; }</code>
然后构造树形结构
<code>foreach ($tree as $id => $row) { if (! isset($row['_childs'])) $tree[$id]['_childs'] = []; if (! empty($row['pid'])) { $tree[$row['pid']]['_childs'][] = &$tree[$id]; } } //现在包含了冗余数据 print_r($tree);</code>
可以去掉冗余部分,也可以留着
<code>foreach ($tree as $id => $row) { if (! empty($row['pid'])) unset($tree[$id]); } print_r($tree);</code>