Home  >  Article  >  Backend Development  >  多级数组遍历问题

多级数组遍历问题

WBOY
WBOYOriginal
2016-06-23 13:27:561096browse

PHP新手请教各位一个问题,现已通过递归从数据库中抓出来一个员工的所有下属(包括下属的下属),print_r ($sub)效果如下:

Array(    [0] => User Object        (            [userId] => 365            [name] => aa            [cn_name] => 王A            [job] => A区域销售工程师            [rep_id] => 368            [sub] => Array                (                )        )    [1] => User Object        (            [userId] => 367            [name] => cc            [cn_name] => 王C            [job] => A区域销售主管            [rep_id] => 368            [sub] => Array                (                    [0] => User Object                        (                            [userId] => 379                            [name] => mm                            [cn_name] => 黄XX                            [job] => 销售工程师                            [rep_id] => 367                            [sub] => Array                                (                                    [0] => User Object                                        (                                            [userId] => 381                                            [name] => abc                                            [cn_name] => 张abc                                            [job] => 销售工程师                                            [rep_id] => 379                                            [sub] => Array                                                (                                                )                                        )                                )                        )                    [1] => User Object                        (                            [userId] => 380                            [name] => xxxx                            [cn_name] => 张XX                            [job] => 销售工程师                            [rep_id] => 367                            [sub] => Array                                (                                )                        )                )        ))



现在的需求是,写一个函数,遍历这个多级数组,然后生成一个新的一维数组,类似这样:
Array(    [0] => User Object        (            [userId] => 365            [name] => aa            [cn_name] => 王A            [job] => A区域销售工程师            [rep_id] => 368        )    [1] => User Object        (            [userId] => 367            [name] => cc            [cn_name] => 王C            [job] => A区域销售主管            [rep_id] => 368        )    [2] => User Object        (            [userId] => 379            [name] => mm            [cn_name] => 黄XX            [job] => 销售工程师            [rep_id] => 367        )    [3] => User Object        (            [userId] => 381            [name] => abc            [cn_name] => 张abc            [job] => 销售工程师            [rep_id] => 379        )    [4] => User Object        (            [userId] => 380            [name] => xxxx            [cn_name] => 张XX            [job] => 销售工程师            [rep_id] => 367         ))


感觉这是数组的基本应用,但是一下脑子短路了,半天没搞出来,所以求教各位,希望各位大神赐教,拜谢!


回复讨论(解决方案)

都下班过节去了么?苦逼的还要加班。。

用foreach(xxx   as   key=>value)读,新的数组赋值。

用foreach(xxx   as   key=>value)读,新的数组赋值。


这个只能读到第一级元素,第二级以后的就会丢掉了。。

递归产生的,当然也要递归降维

递归产生的,当然也要递归降维


恩是的,还是要用递归和全局变量才行,已经搞定了,谢谢
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn