Php multi-dimensional array traversal _ PHP Tutorial

Source: Internet
Author: User
Php traverses multi-dimensional 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 traversal, arrays in php 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, for more information, see.

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

First look at a traversal of two-dimensional array

The code is as follows:

$ Sports = array (
'Football' => 'good ',
'Canonicalization' => 'very well ',
'Running' => 'not good'
);

Foreach ($ sports as $ key => $ value ){
Echo $ key. ":". $ value ."
";
}
?>

The following shows how to traverse 3D arrays.

Example 1

The code is as follows:

Function arr_fun ($ arr ){
If (is_array ($ arr )){
Foreach ($ arr as $ v ){
If (is_array ($ v )){
Arr_fun ($ v );
} Else {
Echo $ v ."
";
}
}
} Else {
Echo $ arr ."
";
}
}

Example 2

The code is as follows:

/*
*-------------------------------------------------
* Url: www. bKjia. c0m
* Date: 2011-03-09
*-------------------------------------------------
*/
Function arr_foreach ($ arr)
{
If (! Is_array ($ arr ))
{
Return false;
}

Foreach ($ arr as $ key => $ 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 );
?>

Output result

Array
(
[1] => Array
(
[0] => 11
[1] => 12
[2] => 13
[14] => Array
(
[0] = & gt; 141
[1] => 142.
)
)
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
11
12
13
141
142
2
3
4
5

Traverse Two-dimensional arrays using list and each

The code is as follows:

$ Sports = array (
'Football' => 'good ',
'Canonicalization' => 'Www. bKjia. c0m ',
'Running' => 'not good'
);

While (list ($ key, $ value) = each ($ sports )){
Echo $ key. ":". $ value ."
";
}
?>

Struct traverses multi-dimensional arrays. if you need it, you can refer to it. Traverse in php...

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.