首頁 > 後端開發 > php教程 > php导航分类 求大神

php导航分类 求大神

WBOY
發布: 2016-06-06 20:12:54
原創
959 人瀏覽過

下面是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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板