首頁 > 後端開發 > php教程 > php無限極分類生成分類樹的實作方法介紹

php無限極分類生成分類樹的實作方法介紹

黄舟
發布: 2023-03-16 09:26:01
原創
2297 人瀏覽過

這篇文章主要介紹了PHP實現無限極分類生成分類樹的方法,結合實例形式簡單分析了無限極分類的原理與實現方法,涉及PHP數組遍歷與判斷相關操作技巧,需要的朋友可以參考下

本文實例講述了PHP實現無限極分類生成分類樹的方法。分享給大家供大家參考,具體如下:

現在的分類資料庫設計基本上都是:每一個分類有一個id主鍵字段,一個pid指向父類的id,這樣便可實現無限級分類,取出的資料就是如下的格式:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

$arr = array(

  array("id" => 1 , "pid" => 0 , 'cat' => '栏目一'),

  array("id" => 2 , "pid" => 0 , 'cat' => '栏目二'),

  array("id" => 3 , "pid" => 1 , 'cat' => '栏目三'),

  array("id" => 4 , "pid" => 2 , 'cat' => '栏目四'),

  array("id" => 5 , "pid" => 1 , 'cat' => '栏目五'),

  array("id" => 6 , "pid" => 5 , 'cat' => '栏目六'),

  array("id" => 7 , "pid" => 5 , 'cat' => '栏目七'),

  array("id" => 8 , "pid" => 6 , 'cat' => '栏目八'),

  array("id" => 9 , "pid" => 1 , 'cat' => '栏目九'),

  array("id" => 10 , "pid" => 0 , 'cat' => '栏目十'),

  array("id" => 11 , "pid" => 10 , 'cat' => '栏目十一'),

  array("id" => 12 , "pid" => 11 , 'cat' => '栏目十二'),

  array("id" => 13 , "pid" => 2 , 'cat' => '栏目十三'),

  array("id" => 14, "pid" => 13 , 'cat' => '栏目十四')

);

登入後複製

不多說,直接上處理程式碼:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

//生成无限极分类树

function make_tree($arr){

  $refer = array();

  $tree = array();

  foreach($arr as $k => $v){

    $refer[$v['id']] = & $arr[$k]; //创建主键的数组引用

  }

  foreach($arr as $k => $v){

    $pid = $v['pid'];  //获取当前分类的父级id

    if($pid == 0){

      $tree[] = & $arr[$k];  //顶级栏目

    }else{

      if(isset($refer[$pid])){

        $refer[$pid]['subcat'][] = & $arr[$k]; //如果存在父级栏目,则添加进父级栏目的子栏目数组中

      }

    }

  }

  return $tree;

}

登入後複製

測試運行:

1

2

$cat = make_tree($arr);

print_r($cat);

登入後複製

執行結果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

Array

(

  [0] => Array

    (

      [id] => 1

      [pid] => 0

      [cat] => 栏目一

      [subcat] => Array

        (

          [0] => Array

            (

              [id] => 3

              [pid] => 1

              [cat] => 栏目三

            )

          [1] => Array

            (

              [id] => 5

              [pid] => 1

              [cat] => 栏目五

              [subcat] => Array

                (

                  [0] => Array

                    (

                      [id] => 6

                      [pid] => 5

                      [cat] => 栏目六

                      [subcat] => Array

                        (

                          [0] => Array

                            (

                              [id] => 8

                              [pid] => 6

                              [cat] => 栏目八

                            )

                        )

                    )

                  [1] => Array

                    (

                      [id] => 7

                      [pid] => 5

                      [cat] => 栏目七

                    )

                )

            )

          [2] => Array

            (

              [id] => 9

              [pid] => 1

              [cat] => 栏目九

            )

        )

    )

  [1] => Array

    (

      [id] => 2

      [pid] => 0

      [cat] => 栏目二

      [subcat] => Array

        (

          [0] => Array

            (

              [id] => 4

              [pid] => 2

              [cat] => 栏目四

            )

          [1] => Array

            (

              [id] => 13

              [pid] => 2

              [cat] => 栏目十三

              [subcat] => Array

                (

                  [0] => Array

                    (

                      [id] => 14

                      [pid] => 13

                      [cat] => 栏目十四

                    )

                )

            )

        )

    )

  [2] => Array

    (

      [id] => 10

      [pid] => 0

      [cat] => 栏目十

      [subcat] => Array

        (

          [0] => Array

            (

              [id] => 11

              [pid] => 10

              [cat] => 栏目十一

              [subcat] => Array

                (

                  [0] => Array

                    (

                      [id] => 12

                      [pid] => 11

                      [cat] => 栏目十二

                    )

                )

            )

        )

    )

)

登入後複製

以上是php無限極分類生成分類樹的實作方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板