如何使用多维数组格式化print_r输出

时间:2017-03-11 21:45:56

标签: php html multidimensional-array

我有一个多维数组,如下所示,

$March = array (
    "Friday" => array(
            "1st" => 3,
            "2nd" => 10,
            "3rd" => 17,
            "4th" => 24,
            "5th" => 31 
    ),
    "Saturday" => array (
            "1st" => 4,
            "2nd" => 11,
            "3rd" => 18,
            "4th" => 25 
    ),
    "Sunday" => array (
            "1st" => 5,
            "2nd" => 12,
            "3rd" => 19,
            "4th" => 26 
    ),
    "Monday" => array (
            "1st" => 6,
            "2nd" => 13,
            "3rd" => 20,
            "4th" => 27 
    ),
    "Tuesday" => array (
            "1st" => 7,
            "2nd" => 14,
            "3rd" => 21,
            "4th" => 28 
    ),
    "Wednesday" => array (
            "1st" => 8,
            "2nd" => 15,
            "3rd" => 22,
            "4th" => 29 
    ),
    "Thursday" => array (
            "1st" => 9,
            "2nd" => 16,
            "3rd" => 23,
            "4th" => 30 
    ) 

我想以下列格式打印出数组

[Friday] => Array ( [1st] => 3 [2nd] => 10 [3rd] => 17 [4th] => 24 [5th] => 31 )
[Saturday] => Array ( [1st] => 4 [2nd] => 11 [3rd] => 18 [4th] => 25 )
[Sunday] => Array ( [1st] => 5 [2nd] => 12 [3rd] => 19 [4th] => 26 )
[Monday] => Array ( [1st] => 6 [2nd] => 13 [3rd] => 20 [4th] => 27 ) 
[Tuesday] => Array ( [1st] => 7 [2nd] => 14 [3rd] => 21 [4th] => 28 )
[Wednesday] => Array ( [1st] => 8 [2nd] => 15 [3rd] => 22 [4th] => 29 ) 
[Thursday] => Array ( [1st] => 9 [2nd] => 16 [3rd] => 23 [4th] => 30 )

我可以使用print_r($ March)来做到这一点;但它给了我这个输出

Array ( [Friday] => Array ( [1st] => 3 [2nd] => 10 [3rd] => 17 [4th] => 24 [5th] => 31 ) [Saturday] => Array ( [1st] => 4 [2nd] => 11 [3rd] => 18 [4th] => 25 ) [Sunday] => Array ( [1st] => 5 [2nd] => 12 [3rd] => 19 [4th] => 26 ) [Monday] => Array ( [1st] => 6 [2nd] => 13 [3rd] => 20 [4th] => 27 ) [Tuesday] => Array ( [1st] => 7 [2nd] => 14 [3rd] => 21 [4th] => 28 ) [Wednesday] => Array ( [1st] => 8 [2nd] => 15 [3rd] => 22 [4th] => 29 ) [Thursday] => Array ( [1st] => 9 [2nd] => 16 [3rd] => 23 [4th] => 30 ) ) 

我已经发现我可以使用print_r($March['Friday']);

打印出每个阵列

但那只能给我这个

Array ( [1st] => 3 [2nd] => 10 [3rd] => 17 [4th] => 24 [5th] => 31 ) 

与此

不一样
[Friday] => Array ( [1st] => 3 [2nd] => 10 [3rd] => 17 [4th] => 24 [5th] => 31 )

发送帮助,一直盯着w3几个小时。

1 个答案:

答案 0 :(得分:0)

要获得您在评论中所写的内容,您可以使用以下循环:

foreach($March as $nameOfDay => $dayArray ){
    foreach($dayArray as $key => $val){
        echo $val.' is the '.$key.' '.$nameOfDay.' in March. ';
    }
}