dongxun6458 2017-03-11 21:45
浏览 88
已采纳

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

I have a multi dimensional array as follows,

$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 
    ) 

)

I would like to print the array out in the following format

[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 )

I can do this by using print_r($March); but it gives me this output

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 ) ) 

I've figured out that I can print each of my arrays out with print_r($March['Friday']);

But that only gives me this

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

which is not the same as this

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

plz send help, have been staring at w3 for hours.

  • 写回答

1条回答 默认 最新

  • dongyu1918 2017-03-11 22:18
    关注

    To achive this what you write in comment you can use below loop:

    foreach($March as $nameOfDay => $dayArray ){
        foreach($dayArray as $key => $val){
            echo $val.' is the '.$key.' '.$nameOfDay.' in March. ';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义