Variation traversal of multidimensional arrays

Source: Internet
Author: User

<?php
02 /*
03 *  -------------------------------------------------
04 *   Author : nowamagic
05 *   Url    : www.nowamagic.net
06 *   Date   : 2011-03-09
07 *  -------------------------------------------------
08 */
09 functionarr_foreach ($arr)
10 {
11     if(!is_array($arr))
12     {
13         returnfalse;
14     }
15     
16     foreach($arr as $key => $val)
17     {
18         if(is_array($val))
19         {
20             arr_foreach ($val);
21         }
22         else
23         {
24             echo$val.‘<br/>‘;
25         }
26     }
27 }
28
29 $arr1= array(1=>array(11,12,13,14=>array(141,142)),2,3,4,5);
30
31 echo‘<pre>‘;
32 print_r($arr1);
33 echo‘<pre>‘;
34
35 arr_foreach ($arr1);
36 ?>

The result of the program operation is:

View SourcePrint?
01 Array
02 (
03     [1] => Array
04         (
05             [0] => 11
06             [1] => 12
07             [2] => 13
08             [14] => Array
09                 (
10                     [0] => 141
11                     [1] => 142
12                 )
13
14         )
15
16     [2] => 2
17     [3] => 3
18     [4] => 4
19     [5] => 5
20 )
21 11
22 12
23 13
24 141
25 142
26 2
27 3
28 4
29 5

Variation traversal of multidimensional arrays

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.