Multi-level array traversal

Source: Internet
Author: User
For more information about multi-level array traversal, see the following figure:
Array ([0] => User Object ([userId] => 365 [name] => aa [cn_name] => Wang A [job] => A region sales engineer [rep_id] => 368 [sub] => Array ()) [1] => User Object ([userId] => 367 [name] => cc [cn_name] => Wang C [job] => area a sales director [rep_id] => 368 [sub] => Array ([0] => User Object ([userId] => 379 [name] => mm [cn_name] => Huang XX [job] => sales engineer [rep_id] => 367 [sub] => Array ([0] => User Object ([userId] => 381 [name] => abc [cn_name] => Zhang abc [job] => sales engineer [rep_id] => 379 [sub] => Array ()))) [1] => User Object ([userId] => 380 [name] => xxxx [cn_name] => Zhang XX [job] => sales engineer [rep_id] => 367 [sub] => Array ()))))



Now, you need to write a function, traverse this multi-level array, and generate a new one-dimensional array, like this:
Array ([0] => User Object ([userId] => 365 [name] => aa [cn_name] => Wang A [job] => A region sales engineer [rep_id] => 368) [1] => User Object ([userId] => 367 [name] => cc [cn_name] => Wang C [job] => area a sales director [rep_id] => 368) [2] => User Object ([userId] => 379 [name] => mm [cn_name] => Huang 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] => xxxx [cn_name] => Zhang XX [job] => sales engineer [rep_id] => 367 ))


I feel like this is the basic application of arrays, but my mind is short-circuited and I haven't figured it out for a long time. so I hope you can give me some advice and thank you!


Reply to discussion (solution)

Have you been off duty for the holidays? I have to work overtime ..

Read with foreach (xxx as key => value) and assign values to the new array.

Read with foreach (xxx as key => value) and assign values to the new array.


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

Recursive dimensionality reduction is also required.

Recursive dimensionality reduction is also required.


Well, we still need to use recursion and global variables. we have done it. thank you.

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.