Multi-Series Group traversal problem

Source: Internet
Author: User
PHP Novice to ask you a question, has now been captured from the database by recursion all subordinates of an employee (including subordinate subordinates), Print_r ($sub) effect is as follows:
Array ([0] = User Object ([userId] = 365 [name] = AA [Cn_name] =&G T                Wang a [job] + a regional sales engineer [rep_id] = 368 [Sub] = = Array ( )) [1] = = User Object ([userId] = 367 [name] + cc [cn_n                    AME] + Wang C [job] + a regional sales Director [REP_ID] = 368 [Sub] + = Array (                            [0] = = User Object ([userId] = 379 [Name] = + mm [Cn_name] = yellow xx [job] + sales Project                                Division [REP_ID] = 367 [Sub] = = Array                                          ([0] = = User Object (  [UserId] = 381 [name] = ABC                                            [Cn_name] = Zhang ABC [job] and sales engineer                                                [rep_id] = 379 [Sub] = = Array                                (                                                )                                        )                            )) [1] = = User Object (                            [UserId] = 380 [name] + xxxx [cn_name] = x [Job] = Sales Engineer [rep_id] = 367 [Sub]        = = Array ())) ))



The need now is to write a function, traverse the multi-series group, and then generate a new one-dimensional array, similar to this:
Array ([0] = User Object ([userId] = 365 [name] = AA [Cn_name] =&G T Wang a [job] + a regional sales engineer [rep_id] = 368) [1] = = User Object ([US Erid] = 367 [name] + cc [Cn_name] + Wang C [job] + a regional sales director [REP_ID            ] = = 368) [2] = = User Object ([userId] = 379 [Name] = = mm             [Cn_name] = yellow xx [job] = Sales Engineer [rep_id] = 367) [3] = = User Object (            [UserId] = 381 [name] + ABC [cn_name]--Zhang ABC [job]--Sales Engineer [rep_id] = 379) [4] = = User Object ([userId] = 380 [name] =&G T xxxx [Cn_name] = Zhang xx [job] = Sales Engineer [rep_id] + 367))


Feel this is the basic application of the array, but the brain short-circuiting, half a day did not get out, so ask you, I hope you great God enlighten, humbly!


Reply to discussion (solution)

Have you been out of work for the holidays? The bitter force also has to work overtime.

Read with foreach (xxx as Key=>value), and the new array is assigned a value.

Read with foreach (xxx as Key=>value), and the new array is assigned a value.


This can only be read to the first level of elements, the second level will be lost.

Recursive generation, of course, also to hand their oaths dimension

Recursive generation, of course, also to hand their oaths dimension


Well, yes, we're going to have to use recursion and global variables.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.