對ecshop中的無限級分類的分析

來源:互聯網
上載者:User
  1. function cat_options($spec_cat_id, $arr)

  2. {
  3. static $cat_options = array();
  4. if (isset($cat_options[$spec_cat_id]))
  5. {
  6. return $cat_options[$spec_cat_id];
  7. }
  8. /*
  9. 初始化關鍵參數:
  10. $level:當前子節點深度
  11. $last_cat_id:當前父節點ID
  12. $options:帶有縮排層級的數組
  13. $cat_id_array:沿同一路徑的父節點依次進駐
  14. $level_array:該節點的子節點深度,也是依次進駐
  15. */
  16. if (!isset($cat_options[0]))
  17. {
  18. $level = $last_cat_id = 0;
  19. $options = $cat_id_array = $level_array = array();
  20. while (!empty($arr))//如果還有待構造的節點則繼續遍曆
  21. {
  22. foreach ($arr AS $key => $value)
  23. {
  24. $cat_id = $value['cat_id'];
  25. //一級分類結點
  26. if ($level == 0 && $last_cat_id == 0)
  27. {
  28. if ($value['parent_id'] > 0)
  29. {
  30. break;
  31. }
  32. $options[$cat_id] = $value;
  33. $options[$cat_id]['level'] = $level;
  34. $options[$cat_id]['id'] = $cat_id;
  35. $options[$cat_id]['name'] = $value['cat_name'];
  36. //遍曆過了就不再遍曆
  37. unset($arr[$key]);
  38. if ($value['has_children'] == 0)
  39. {
  40. continue;
  41. }
  42. $last_cat_id = $cat_id;//下層結點的父親結點
  43. $cat_id_array = array($cat_id);
  44. $level_array[$last_cat_id] = ++$level;
  45. continue;
  46. }
  47. //當前結點的父親結點ID等於它的上一級結點ID
  48. if ($value['parent_id'] == $last_cat_id)
  49. {
  50. $options[$cat_id] = $value;
  51. $options[$cat_id]['level'] = $level;
  52. $options[$cat_id]['id'] = $cat_id;
  53. $options[$cat_id]['name'] = $value['cat_name'];
  54. unset($arr[$key]);//遍曆過了就不再遍曆
  55. //如果當前結點有孩子則當前結點要進駐,但不再遍曆;反之不進駐也不再遍曆
  56. if ($value['has_children'] > 0)
  57. {
  58. if (end($cat_id_array) != $last_cat_id)
  59. {
  60. $cat_id_array[] = $last_cat_id;
  61. }
  62. $last_cat_id = $cat_id;//當現結點做為下一級結點的新的父親結點
  63. $cat_id_array[] = $cat_id;//進駐

  64. $level_array[$last_cat_id] = ++$level;//當前結點的下一級結點深度

  65. }

  66. }

  67. elseif ($value['parent_id'] > $last_cat_id)
  68. {//如果當前結點父親深度大於目前父親結點的深度則進行下一輪迴圈
  69. break;
  70. }
  71. }//endforeach
  72. $count = count($cat_id_array);
  73. if ($count > 1)
  74. {
  75. //取出最後進駐的父親節點作為當前父親節點
  76. $last_cat_id = array_pop($cat_id_array);
  77. }
  78. elseif ($count == 1)
  79. {
  80. if ($last_cat_id != end($cat_id_array))
  81. {
  82. //進駐的父親結點只有一個時並且沒有作為當前父親節點時把它取出
  83. $last_cat_id = end($cat_id_array);
  84. }
  85. else
  86. { //否則最後取出的父親結點一定是一級分類結點
  87. $level = 0;
  88. $last_cat_id = 0;
  89. $cat_id_array = array();
  90. continue;
  91. }
  92. }

  93. if ($last_cat_id && isset($level_array[$last_cat_id]))

  94. {
  95. //取出當前結點的深度
  96. $level = $level_array[$last_cat_id];
  97. }
  98. else
  99. {
  100. $level = 0;
  101. }
  102. }//end while,此時已完成非遞迴前序走訪構造樹的工作,其中$options已儲存了從根結點開始的所有結點帶有分層性質的數組
  103. $cat_options[0] = $options;
  104. }
  105. else
  106. {
  107. $options = $cat_options[0];
  108. }
  109. //如果從0開始即取整個樹則直接返回不再處理.
  110. if (!$spec_cat_id)
  111. {
  112. return $options;
  113. }
  114. //否則開始從指定結點截取,以下比較簡單我還是稍微說說吧,要說就說幾個參數含義吧
  115. /*
  116. $spec_cat_id_level:截取結點的深度
  117. $spec_cat_id_array:最終返回的以該結點為根結點的一棵商品分類樹
  118. 最終返回的數組是這樣排序的:按父親結點大小,按直接父親結點,按同一父親結點這樣的先根遍曆,具個例子:
  119. 一級結點有1,5 二級結點有2,6,7 三級結點有8,9,如果1的直接孩子是2,6而2的直接孩子是8,9;另外
  120. 5的直接孩子是7那麼最終的數組是這樣排列的1->2->8->9->6->5->7
  121. */
  122. else
  123. {
  124. if (empty($options[$spec_cat_id]))
  125. {
  126. return array();
  127. }
  128. $spec_cat_id_level = $options[$spec_cat_id]['level'];

  129. foreach ($options AS $key => $value)

  130. {
  131. if ($key != $spec_cat_id)
  132. {
  133. unset($options[$key]);
  134. }
  135. else
  136. {
  137. break;
  138. }
  139. }
  140. $spec_cat_id_array = array();
  141. foreach ($options AS $key => $value)
  142. {
  143. if (($spec_cat_id_level == $value['level'] && $value['cat_id'] != $spec_cat_id) ||
  144. ($spec_cat_id_level > $value['level']))
  145. {
  146. break;
  147. }
  148. else
  149. {
  150. $spec_cat_id_array[$key] = $value;
  151. }
  152. }
  153. $cat_options[$spec_cat_id] = $spec_cat_id_array;
  154. return $spec_cat_id_array;
  155. }
  156. }
  157. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.