PHP traversal method of multi-dimensional array_PHP tutorial

WBOY
Release: 2016-07-13 10:44:41
Original
909 people have browsed it

There are many types of arrays in PHP, such as one-dimensional arrays, two-dimensional arrays, three-dimensional or multi-dimensional arrays. Now I will introduce to you how to traverse multi-dimensional arrays in PHP. Friends in need can refer to it.

There are three functions foreach, each, list & each in PHP to traverse arrays. Let’s see how they traverse multi-dimensional arrays.

Let’s first look at traversing 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."
"; 

?>

$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good'
);
                         
foreach ($sports as $key => $value) {
echo $key.": ".$value."
";
}
?>

Look below at traversing a three-dimensional array

Example 1
 代码如下 复制代码

 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."
";
       }
   }

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."
";
}
}

例2

输出结果
 代码如下
 代码如下 复制代码

/*
* -------------------------------------------------
* 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)),2,3,4,5);
echo '

';
print_r($arr1);
echo '
';
arr_foreach ($arr1);
?>

输出结果

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

复制代码

 代码如下 复制代码

$sports = array(
'football' => 'good',
    'swimming' => 'www.bKjia.c0m',
    'running'  => 'not good'
);

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

/*
*  -------------------------------------------------
*   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)),2,3,4,5);
echo '
';
print_r($arr1);
echo '
';
arr_foreach ($arr1);
?>
Array
(
    [1] => Array
        (
            [0] => 11
            [1] => 12
            [2] => 13
            [14] => Array
                (
                    [0] => 141
                    [1] => 142
                )
        )
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)
11
12
13
141
142
2
3
4
5

利用list与each遍历二维数组
 代码如下 复制代码
$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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633076.htmlTechArticle在php中数组分类很多种,如一维数组,二维数组,三维或多维数组,下面我来给大家介绍php 遍历多维数组,有需要的朋友可参考。 在php遍历...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template