php数组 id fid 分类函数

View Code
  1 <meta content="text/html" charset="utf-8"/>
  2 <?php
  3 $a=array (
  4   0 =>
  5   array (
  6     'id' => '134',
  7     'fid' => '0',
  8     'name' => 't',
  9   ),
 10   1 =>
 11   array (
 12     'id' => '133',
 13     'fid' => '0',
 14     'name' => 'k',
 15   ),
 16   2 =>
 17   array (
 18     'id' => '132',
 19     'fid' => '0',
 20     'name' => 'j',
 21   ),
 22   3 =>
 23   array (
 24     'id' => '137',
 25     'fid' => '133',
 26     'name' => '我是k的分类1',
 27   ),
 28   4 =>
 29   array (
 30     'id' => '136',
 31     'fid' => '134',
 32     'name' => '我是t的分类t2',
 33   ),
 34   5 =>
 35   array (
 36     'id' => '135',
 37     'fid' => '134',
 38     'name' => '我是t的分类t1',
 39   ),
 40   6 =>
 41   array (
 42     'id' => '138',
 43     'fid' => '137',
 44     'name' => '我是k的分类1-1',
 45   ),
 46   7 =>
 47   array (
 48     'id' => '139',
 49     'fid' => '138',
 50     'name' => '我是k的分类1-1-1',
 51   ),
 52   8 =>
 53   array (
 54     'id' => '140',
 55     'fid' => '139',
 56     'name' => '我是k的分类1-1-1-1',
 57   ),
 58 );
 59 
 60 
 61 $r = array (
 62   0 =>
 63   array (
 64     'id' => '134',
 65     'fid' => '0',
 66     'name' => 't',
 67     'children' =>
 68     array (
 69       0 =>
 70       array (
 71         'id' => '136',
 72         'fid' => '134',
 73         'name' => '我是t的分类t2',
 74         'children' => NULL,
 75       ),
 76       1 =>
 77       array (
 78         'id' => '135',
 79         'fid' => '134',
 80         'name' => '我是t的分类t1',
 81         'children' => NULL,
 82       ),
 83     ),
 84   ),
 85   1 =>
 86   array (
 87     'id' => '133',
 88     'fid' => '0',
 89     'name' => 'k',
 90     'children' =>
 91     array (
 92       0 =>
 93       array (
 94         'id' => '137',
 95         'fid' => '133',
 96         'name' => '我是k的分类1',
 97         'children' =>
 98         array (
 99           0 =>
100           array (
101             'id' => '138',
102             'fid' => '137',
103             'name' => '我是k的分类1-1',
104             'children' =>
105             array (
106               0 =>
107               array (
108                 'id' => '139',
109                 'fid' => '138',
110                 'name' => '我是k的分类1-1-1',
111                 'children' =>
112                 array (
113                   0 =>
114                   array (
115                     'id' => '140',
116                     'fid' => '139',
117                     'name' => '我是k的分类1-1-1-1',
118                     'children' => NULL,
119                   ),
120                 ),
121               ),
122             ),
123           ),
124         ),
125       ),
126     ),
127   ),
128   2 =>
129   array (
130     'id' => '132',
131     'fid' => '0',
132     'name' => 'j',
133     'children' => NULL,
134   ),
135 );
136 
137 //$a ==>> $r
138 echo "<pre>";
139 var_dump(assort($a, 0));
140 echo '</pre>';
141 //分类数组
142 function assort(& $arr, $fid = '0') {
143     //获取直接子类
144     $r = array();
145     foreach($arr as $k => $v) {
146         if($v['fid'] == $fid) {
147             $r[] = $v;
148             unset($arr[$k]);
149         }
150     }
151     if(empty($r)) {
152         return null;
153     }
154     //对不为空的直接子类进行循环添加子类
155     foreach($r as $k => $v) {
156         $r[$k]['children'] = assort($arr, $v['id']);
157     }
158     return $r;
159 }
160 
161 
162 ?>

 

posted @ 2013-02-28 08:59  Caps  阅读(464)  评论(0编辑  收藏  举报