Php multi-dimensional array traversal method

Source: Internet
Author: User
In php, arrays are classified into many types, such as one-dimensional arrays, two-dimensional arrays, and three-dimensional or multi-dimensional arrays. next I will introduce php to traverse multi-dimensional arrays. if you need them, refer to them. In php, there are three traversal arrays

In php, arrays are classified into many types, such as one-dimensional arrays, two-dimensional arrays, and three-dimensional or multi-dimensional arrays. next I will introduce php to traverse multi-dimensional arrays. if you need them, refer to them.

In php, there are three functions for traversing arrays: foreach, each, list, and each. let's see how they traverse multi-dimensional arrays.

First, let's look at a traversal of a two-dimensional array. the code is as follows:

  1. $ Sports = array (
  2. 'Football' => 'good ',
  3. 'Canonicalization' => 'very well ',
  4. 'Running' => 'not good'
  5. );
  6. Foreach ($ sports as $ key => $ value ){
  7. Echo $ key. ":". $ value ." ";
  8. }
  9. ?>

The following shows how to traverse 3D arrays.

Example 1 code:

  1. Function arr_fun ($ arr ){
  2. If (is_array ($ arr )){
  3. Foreach ($ arr as $ v ){
  4. If (is_array ($ v )){
  5. Arr_fun ($ v );
  6. } Else {
  7. Echo $ v ."
    ";
  8. }
  9. }
  10. } Else {
  11. Echo $ arr ."
    ";
  12. }
  13. }

Example 2 code:

  1. /*
  2. *-------------------------------------------------
  3. * Url: www.phpfensi.com
  4. * Date: 2011-03-09
  5. *-------------------------------------------------
  6. */
  7. Function arr_foreach ($ arr)
  8. {
  9. If (! Is_array ($ arr ))
  10. {
  11. Return false;
  12. }
  13.  
  14. Foreach ($ arr as $ key => $ val)
  15. {
  16. If (is_array ($ val ))
  17. {
  18. Arr_foreach ($ val );
  19. }
  20. Else
  21. {
  22. Echo $ val .'
    ';
  23. }
  24. }
  25. }
  26. $ Arr1 = array (1 => array (11,12, 13,14 => array (141,142 );
  27. Echo'
    '; 
  28. Print_r ($ arr1 );
  29. Echo'
    '; 
  30. Arr_foreach ($ arr1 );
  31. ?>
  32. Output result
  33. Array
  34. (
  35. [1] => Array
  36. (
  37. [0] => 11
  38. [1] => 12
  39. [2] => 13
  40. [14] => Array
  41. (
  42. [0] = & gt; 141
  43. [1] => 142.
  44. )
  45. )
  46. [2] => 2
  47. [3] => 3
  48. [4] => 4
  49. [5] => 5
  50. )

Use list and each to traverse two-dimensional arrays. the code is as follows:

  1. $ Sports = array (
  2. 'Football' => 'good ',
  3. 'Canonicalization' => 'Www .phpfensi.com ',
  4. 'Running' => 'not good'
  5. );
  6. While (list ($ key, $ value) = each ($ sports )){
  7. Echo $ key. ":". $ value ." ";
  8. }
  9. ?>

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.