PHP traversal multidimensional Array Method _php Tutorial

Source: Internet
Author: User
In the PHP array classification of many kinds, such as a dimensional array, two-dimensional array, three-dimensional or multidimensional array, let me introduce you to PHP traversal multidimensional array, the need for friends to refer to.

In the PHP traversal array There are three functions foreach, each, list & each below we see how they traverse the multidimensional array.

Let's look at a two-dimensional array.

The code is as follows Copy Code

$sports = Array (
' Football ' = ' good ',
' Swimming ' = ' very well ',
' Running ' = ' not good '
);

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

Let's look at the three-dimensional array

Example 1

The code is as follows Copy Code

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 Copy Code

!--? php
/*
*-------------------------------------------------
* 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. '
';
}
}
}
$arr 1 = Array (1=>array (11,12,13,14=>array (141,142)), 2,3,4,5);
Echo '

';
Print_r ($arr 1);
Echo '
';
Arr_foreach ($arr 1);
?>

Output results

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

Using list and each to traverse a two-dimensional array

The code is as follows Copy Code

$sports = Array (
' Football ' = ' good ',
' Swimming ' = ' www.bKjia.c0m ',
' Running ' = ' not good '
);

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

http://www.bkjia.com/PHPjc/633076.html www.bkjia.com true http://www.bkjia.com/PHPjc/633076.html techarticle in the PHP array classification of many kinds, such as a dimensional array, two-dimensional array, three-dimensional or multidimensional array, let me introduce you to PHP traversal multidimensional array, the need for friends to refer to. 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.