php無限級分類_PHP教程

來源:互聯網
上載者:User
CI的seven給我提供了一個方法,如下:[php] array('id' => 1, 'pid' => 0, 'name' => '江西省'), 2 => array('id' => 2, 'pid' => 0, 'name' => '黑龍江省'), 3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'), 4 => array('id' => 4, 'pid' => 2, 'name' => '哈爾濱市'), 5 => array('id' => 5, 'pid' => 2, 'name' => '雞西市'), 6 => array('id' => 6, 'pid' => 4, 'name' => '香坊區'), 7 => array('id' => 7, 'pid' => 4, 'name' => '南崗區'), 8 => array('id' => 8, 'pid' => 6, 'name' => '和興路'), 9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'), 10 => array('id' => 10, 'pid' => 8, 'name' => '東北林業大學'), 11 => array('id' => 11, 'pid' => 9, 'name' => '哈爾濱工業大學'), 12 => array('id' => 12, 'pid' => 8, 'name' => '哈爾濱師範大學'), 13 => array('id' => 13, 'pid' => 1, 'name' => '贛州市'), 14 => array('id' => 14, 'pid' => 13, 'name' => '贛縣'), 15 => array('id' => 15, 'pid' => 13, 'name' => '于都縣'), 16 => array('id' => 16, 'pid' => 14, 'name' => '茅店鎮'), 17 => array('id' => 17, 'pid' => 14, 'name' => '大田鄉'), 18 => array('id' => 18, 'pid' => 16, 'name' => '義源村'), 19 => array('id' => 19, 'pid' => 16, 'name' => '上壩村'), ); echo "
";  print_r(genTree5($items));  print_r(genTree9($items));    //後者輸出格式,前者類似,只是數組索引值不一樣,不過不影響資料結構  /*  Array  (  [0] => Array      (          [id] => 1          [pid] => 0          [name] => 江西省          [son] => Array              (                  [0] => Array                      (                          [id] => 3                          [pid] => 1                          [name] => 南昌市                      )                    [1] => Array                      (                          [id] => 13                          [pid] => 1                          [name] => 贛州市                          [son] => Array                              (                                  [0] => Array                                      (                                          [id] => 14                                          [pid] => 13                                          [name] => 贛縣                                          [son] => Array                                              (                                              [0] => Array                                                  (                                                      [id] => 16                                                      [pid] => 14                                                      [name] => 茅店鎮                                                      [son] => Array                                                          (                                                          [0] => Array                                                              (                                                              [id] => 18                                                              [pid] => 16                                                              [name] => 義源村                                                              )                                                            [1] => Array                                                              (                                                              [id] => 19                                                              [pid] => 16                                                              [name] => 上壩村                                                              )                                                            )                                                    )                                                [1] => Array                                                  (                                                      [id] => 17                                                      [pid] => 14                                                      [name] => 大田鄉                                                  )                                                )                                        )                                    [1] => Array                                      (                                          [id] => 15                                          [pid] => 13                                          [name] => 于都縣                                      )                                )                        )                )        )    [1] => Array      (          [id] => 2          [pid] => 0          [name] => 黑龍江省          [son] => Array              (                  [0] => Array                      (                          [id] => 4                          [pid] => 2                          [name] => 哈爾濱市                          [son] => Array                              (                              [0] => Array                                  (                                      [id] => 6                                      [pid] => 4                                      [name] => 香坊區                                      [son] => Array                                          (                                          [0] => Array                                              (                                                  [id] => 8                                                  [pid] => 6                                                  [name] => 和興路                                                  [son] => Array                                                      (                                                          [0] => Array                                                              (                                                              [id] => 10                                                              [pid] => 8                                                              [name] =>                                                                東北林業大學                                                              )                                                            [1] => Array                                                              (                                                              [id] => 12                                                              [pid] => 8                                                              [name] =>                                                               哈爾濱師範大學                                                              )                                                        )                                                )                                            )                                    )                                [1] => Array                                  (                                      [id] => 7                                      [pid] => 4                                      [name] => 南崗區                                      [son] => Array                                          (                                          [0] => Array                                              (                                              [id] => 9                                              [pid] => 7                                              [name] => 西大直街                                              [son] => Array                                                  (                                                  [0] => Array                                                      (                                                      [id] => 11                                                      [pid] => 9                                                      [name] =>                                                        哈爾濱工業大學                                                      )                                                    )                                                )                                            )                                    )                                )                        )                    [1] => Array                      (                          [id] => 5                          [pid] => 2                          [name] => 雞西市                      )                )        )  )*    通過測試,能夠完成所設想的分類無限級,但是,用他自有資料可以,但是用我資料庫的資料不可以,會出現錯誤,比如會出現如下錯誤:[php]  Array  (      [10] => Array          (              [child] => Array                  (                      [14] =>                   )            )        [11] =>       [12] =>       [9] => Array          (              [child] => Array                  (                      [13] =>                   )            )    )   只顯示數組序號,不顯示相應資料,這是我很苦惱,這樣測試後能夠完全顯示: [php]  function genTree5($items) {       foreach ($items as $item) {      $items[$item['id']]=$item;//ADD THIS          $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];           }      return isset($items[0]['son']) ? $items[0]['son'] : array();   }    但是這隻在id升序狀態下可以使用,亂序會顯示錯誤 雖然在大多情況下id會保持升序狀態,但是如果涉及到排序後,遍無法使用了。之前,CI的呂老師提供了一種高效方法,即: [php] id 父id   分級     1   0      0,1  2   1     0,1,2  3   1     0,1,3  4   3    0,1,3,4    select * from 表  order  by 分級 asc   這個結構,會使分級按照父子順序排序,首先對第一個0排序,然後對第二個1排序,以此類推,自動分類,十分好用(同時注意,一定要保持有兩組,也就是0,1開始或者0,0開始,否則根分級會提前顯示)。“分級”也可以叫childid,因為是記錄的子id層。[php]  |--2  |----2-1  |------2-1-1  |--3  |----3-1  |--4  |--1  |----1-1  顯示直接遍曆即可,因為我有一個depth欄位,記錄深度,所以能更好的顯示。這個方法已耗用時間也較短,微秒級運行,總之耗費在資料庫排序上了,其他的基本很愉快。利用指標的一般在沒有緩衝情況下測試大多數大於排序後的時間,大多數是CPU時間,而且,結果不準確。 PS:PHP已經對地址,指標之類的概念弱化了,也就是說,並不是PHP的特點,雖然可以使用,但是會出現一些莫名錯誤。所以還是老老實實的吧~神馬大資料演算法,就不用PHP來做了,C++什麼的,幹這個,比PHP速度快多了。。。。同時 @Tonton 對他的演算法進行修正。 

http://www.bkjia.com/PHPjc/477766.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477766.htmlTechArticleCI的seven給我提供了一個方法,如下: [php] ?php /** * 此方法由@Tonton 提供 * http://my.oschina.net/u/918697 * @date 2012-12-12 */ function genTree5($items) { fore...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.