Recursive traversal of PHP multi-dimensional arrays _ PHP Tutorial

Source: Internet
Author: User
Recursively traverse PHP multi-dimensional arrays. Array traversal is a common programming task in PHP. arrays are classified into one-dimensional arrays, two-dimensional arrays, and multi-dimensional arrays. One-dimensional array traversal is very simple. it is a common programming task in PHP to traverse arrays through a for loop directly. arrays are classified into one-dimensional arrays, two-dimensional arrays, and multi-dimensional arrays. The traversal of one-dimensional arrays is simple, and a for loop can be completed directly. How can we implement the traversal of two-dimensional arrays and multi-dimensional arrays? See the following program:

 $ Val) {if (is_array ($ val) {arr_foreach ($ val);} else {echo $ val .'
';}}$ Arr1 = array (1 => array (11,12, 13,14 => array (141,142),); echo'
'; Print_r ($ arr1); echo'
';arr_foreach ($arr1);?>

The program running result is:

Array(    [1] => Array        (            [0] => 11            [1] => 12            [2] => 13            [14] => Array                (                    [0] => 141                    [1] => 142                )        )    [2] => 2    [3] => 3    [4] => 4    [5] => 5)1112131411422345

The program is very simple. let's take a look at the advantages of recursion.

Bytes. The traversal of one-dimensional arrays is very simple, and a for loop can be completed directly...

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.