注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

php开发lamp

《西安--木木》-经历丰富了生活。 架构师QQ群: 246695517

 
 
 

日志

 
 

无限分类数据树形格式化  

2013-07-17 23:17:47|  分类: php基础 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
001<?php
002 
003/**
004 * 此方法由@Tonton 提供
005 * http://my.oschina.net/u/918697
006 * @date 2012-12-12
007 */
008function genTree5($items) {
009    foreach ($items as $item)
010        $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
011    return isset($items[0]['son']) ? $items[0]['son'] : array();
012}
013 
014/**
015 * 将数据格式化成树形结构
016 * @author Xuefen.Tong
017 * @param array $items
018 * @return array
019 */
020function genTree9($items) {
021    $tree = array(); //格式化好的树
022    foreach ($items as $item)
023        if (isset($items[$item['pid']]))
024            $items[$item['pid']]['son'][] = &$items[$item['id']];
025        else
026            $tree[] = &$items[$item['id']];
027    return $tree;
028}
029 
030$items = array(
031    1 => array('id' => 1, 'pid' => 0, 'name' => '江西省'),
032    2 => array('id' => 2, 'pid' => 0, 'name' => '黑龙江省'),
033    3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'),
034    4 => array('id' => 4, 'pid' => 2, 'name' => '哈尔滨市'),
035    5 => array('id' => 5, 'pid' => 2, 'name' => '鸡西市'),
036    6 => array('id' => 6, 'pid' => 4, 'name' => '香坊区'),
037    7 => array('id' => 7, 'pid' => 4, 'name' => '南岗区'),
038    8 => array('id' => 8, 'pid' => 6, 'name' => '和兴路'),
039    9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'),
040    10 => array('id' => 10, 'pid' => 8, 'name' => '东北林业大学'),
041    11 => array('id' => 11, 'pid' => 9, 'name' => '哈尔滨工业大学'),
042    12 => array('id' => 12, 'pid' => 8, 'name' => '哈尔滨师范大学'),
043    13 => array('id' => 13, 'pid' => 1, 'name' => '赣州市'),
044    14 => array('id' => 14, 'pid' => 13, 'name' => '赣县'),
045    15 => array('id' => 15, 'pid' => 13, 'name' => '于都县'),
046    16 => array('id' => 16, 'pid' => 14, 'name' => '茅店镇'),
047    17 => array('id' => 17, 'pid' => 14, 'name' => '大田乡'),
048    18 => array('id' => 18, 'pid' => 16, 'name' => '义源村'),
049    19 => array('id' => 19, 'pid' => 16, 'name' => '上坝村'),
050);
051echo "<pre>";
052print_r(genTree5($items));
053print_r(genTree9($items));
054 
055//后者输出格式,前者类似,只是数组键值不一样,不过不影响数据结构
056/*
057Array
058(
059[0] => Array
060    (
061        [id] => 1
062        [pid] => 0
063        [name] => 江西省
064        [son] => Array
065            (
066                [0] => Array
067                    (
068                        [id] => 3
069                        [pid] => 1
070                        [name] => 南昌市
071                    )
072 
073                [1] => Array
074                    (
075                        [id] => 13
076                        [pid] => 1
077                        [name] => 赣州市
078                        [son] => Array
079                            (
080                                [0] => Array
081                                    (
082                                        [id] => 14
083                                        [pid] => 13
084                                        [name] => 赣县
085                                        [son] => Array
086                                            (
087                                            [0] => Array
088                                                (
089                                                    [id] => 16
090                                                    [pid] => 14
091                                                    [name] => 茅店镇
092                                                    [son] => Array
093                                                        (
094                                                        [0] => Array
095                                                            (
096                                                            [id] => 18
097                                                            [pid] => 16
098                                                            [name] => 义源村
099                                                            )
100 
101                                                        [1] => Array
102                                                            (
103                                                            [id] => 19
104                                                            [pid] => 16
105                                                            [name] => 上坝村
106                                                            )
107 
108                                                        )
109 
110                                                )
111 
112                                            [1] => Array
113                                                (
114                                                    [id] => 17
115                                                    [pid] => 14
116                                                    [name] => 大田乡
117                                                )
118 
119                                            )
120 
121                                    )
122 
123                                [1] => Array
124                                    (
125                                        [id] => 15
126                                        [pid] => 13
127                                        [name] => 于都县
128                                    )
129 
130                            )
131 
132                    )
133 
134            )
135 
136    )
137 
138[1] => Array
139    (
140        [id] => 2
141        [pid] => 0
142        [name] => 黑龙江省
143        [son] => Array
144            (
145                [0] => Array
146                    (
147                        [id] => 4
148                        [pid] => 2
149                        [name] => 哈尔滨市
150                        [son] => Array
151                            (
152                            [0] => Array
153                                (
154                                    [id] => 6
155                                    [pid] => 4
156                                    [name] => 香坊区
157                                    [son] => Array
158                                        (
159                                        [0] => Array
160                                            (
161                                                [id] => 8
162                                                [pid] => 6
163                                                [name] => 和兴路
164                                                [son] => Array
165                                                    (
166                                                        [0] => Array
167                                                            (
168                                                            [id] => 10
169                                                            [pid] => 8
170                                                            [name] =>
171                                                             东北林业大学
172                                                            )
173 
174                                                        [1] => Array
175                                                            (
176                                                            [id] => 12
177                                                            [pid] => 8
178                                                            [name] =>
179                                                            哈尔滨师范大学
180                                                            )
181 
182                                                    )
183 
184                                            )
185 
186                                        )
187 
188                                )
189 
190                            [1] => Array
191                                (
192                                    [id] => 7
193                                    [pid] => 4
194                                    [name] => 南岗区
195                                    [son] => Array
196                                        (
197                                        [0] => Array
198                                            (
199                                            [id] => 9
200                                            [pid] => 7
201                                            [name] => 西大直街
202                                            [son] => Array
203                                                (
204                                                [0] => Array
205                                                    (
206                                                    [id] => 11
207                                                    [pid] => 9
208                                                    [name] =>
209                                                     哈尔滨工业大学
210                                                    )
211 
212                                                )
213 
214                                            )
215 
216                                        )
217 
218                                )
219 
220                            )
221 
222                    )
223 
224                [1] => Array
225                    (
226                        [id] => 5
227                        [pid] => 2
228                        [name] => 鸡西市
229                    )
230 
231            )
232 
233    )
234)*/
  评论这张
 
阅读(187)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017