php导航分类 求大神

WBOY
发布: 2016-06-06 20:12:54
原创
949 人浏览过

下面是10个数组

<code>array(10) {
  [0] => array(5) {
    ["id"] => string(1) "1"
    ["pid"] => string(1) "0"
    ["title"] => string(6) "讨论"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [1] => array(5) {
    ["id"] => string(1) "2"
    ["pid"] => string(1) "0"
    ["title"] => string(6) "下载"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [2] => array(5) {
    ["id"] => string(1) "3"
    ["pid"] => string(1) "0"
    ["title"] => string(9) "官方区"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [3] => array(5) {
    ["id"] => string(1) "4"
    ["pid"] => string(1) "1"
    ["title"] => string(12) "求助交流"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [4] => array(5) {
    ["id"] => string(1) "5"
    ["pid"] => string(1) "1"
    ["title"] => string(12) "技术分享"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [5] => array(5) {
    ["id"] => string(1) "6"
    ["pid"] => string(1) "2"
    ["title"] => string(9) "电子书"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [6] => array(5) {
    ["id"] => string(1) "7"
    ["pid"] => string(1) "2"
    ["title"] => string(6) "软件"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [7] => array(5) {
    ["id"] => string(1) "8"
    ["pid"] => string(1) "3"
    ["title"] => string(6) "反馈"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [8] => array(5) {
    ["id"] => string(1) "9"
    ["pid"] => string(1) "1"
    ["title"] => string(12) "综合讨论"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [9] => array(5) {
    ["id"] => string(2) "10"
    ["pid"] => string(1) "3"
    ["title"] => string(12) "程序发布"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
}</code>
登录后复制
登录后复制

pid为0的是顶级导航 有3个 id为0,1,2 也就是讨论 下载 官方区
pid不为0的是子导航 有7个 id为3,4,5,6,7,8,9 也就是求助交流 技术分享 电子书 软件 反馈 综合讨论 程序发布
子导航的pid就是顶级导航的id 也就是讨论下面是求助交流 技术分享 综合讨论

现在需要写一个函数 参数就是那十堆数组 要求返回一个数组$return

$return['top']就是那些顶级导航数组 3组数组
array(3){..顶级导航`讨论 下载 官方区数组..}

$return['child']就是那些子导航数组 注意的是也是3组数组 也就是和顶级数组对应
array(3){
[0] => array(3){..求助交流 技术分享 综合讨论数组.}
[1] => array(2){..电子书 软件数组..}
[2] => array(2){..反馈 程序发布数组..}
}
数组1属于讨论数组的子导航数组
数组2是下载数组的子导航数组
数组3就是官方区数组的子导航数组

回复内容:

下面是10个数组

<code>array(10) {
  [0] => array(5) {
    ["id"] => string(1) "1"
    ["pid"] => string(1) "0"
    ["title"] => string(6) "讨论"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [1] => array(5) {
    ["id"] => string(1) "2"
    ["pid"] => string(1) "0"
    ["title"] => string(6) "下载"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [2] => array(5) {
    ["id"] => string(1) "3"
    ["pid"] => string(1) "0"
    ["title"] => string(9) "官方区"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [3] => array(5) {
    ["id"] => string(1) "4"
    ["pid"] => string(1) "1"
    ["title"] => string(12) "求助交流"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [4] => array(5) {
    ["id"] => string(1) "5"
    ["pid"] => string(1) "1"
    ["title"] => string(12) "技术分享"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [5] => array(5) {
    ["id"] => string(1) "6"
    ["pid"] => string(1) "2"
    ["title"] => string(9) "电子书"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [6] => array(5) {
    ["id"] => string(1) "7"
    ["pid"] => string(1) "2"
    ["title"] => string(6) "软件"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [7] => array(5) {
    ["id"] => string(1) "8"
    ["pid"] => string(1) "3"
    ["title"] => string(6) "反馈"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [8] => array(5) {
    ["id"] => string(1) "9"
    ["pid"] => string(1) "1"
    ["title"] => string(12) "综合讨论"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
  [9] => array(5) {
    ["id"] => string(2) "10"
    ["pid"] => string(1) "3"
    ["title"] => string(12) "程序发布"
    ["sort"] => string(1) "0"
    ["siatus"] => string(1) "0"
  }
}</code>
登录后复制
登录后复制

pid为0的是顶级导航 有3个 id为0,1,2 也就是讨论 下载 官方区
pid不为0的是子导航 有7个 id为3,4,5,6,7,8,9 也就是求助交流 技术分享 电子书 软件 反馈 综合讨论 程序发布
子导航的pid就是顶级导航的id 也就是讨论下面是求助交流 技术分享 综合讨论

现在需要写一个函数 参数就是那十堆数组 要求返回一个数组$return

$return['top']就是那些顶级导航数组 3组数组
array(3){..顶级导航`讨论 下载 官方区数组..}

$return['child']就是那些子导航数组 注意的是也是3组数组 也就是和顶级数组对应
array(3){
[0] => array(3){..求助交流 技术分享 综合讨论数组.}
[1] => array(2){..电子书 软件数组..}
[2] => array(2){..反馈 程序发布数组..}
}
数组1属于讨论数组的子导航数组
数组2是下载数组的子导航数组
数组3就是官方区数组的子导航数组

<code>function getFormate($arr){
    $top = $child = array();
    foreach($arr as $val){
        if($val['pid'] == 0){
            $top[] = $val;
        }else{
            $child[ $val['pid'] ][] =    $val; 
        }
    }
    return array('top' => $top, 'child' => array_values($child) );
}</code>
登录后复制

这应该是你需要的

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板