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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器