Home  >  Article  >  Backend Development  >  php导航分类 求大神

php导航分类 求大神

WBOY
WBOYOriginal
2016-06-06 20:12:54936browse

下面是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>

这应该是你需要的

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn