doushi3715 2014-09-12 16:12
浏览 47
已采纳

在php中打印格式化的多维数组

I'm trying to output my multi array in a nice format so i can read it easily

Here's my array code:

$weekly_array = array
    (
    "Sunday" =>     array(  "OT" => $sundayOT,
                            "LIEU" => $sundayLIEU,
                            "TOTAL" => $sundayOT+$sundayLIEU,
                            "STAT" => $sundaySTAT,
                            "WEEKDAY" => 0),
    "Monday" =>     array(  "OT" => $mondayOT,
                            "LIEU" => $mondayLIEU,
                            "TOTAL" => $mondayOT+$mondayLIEU,
                            "STAT" => $mondaySTAT,
                            "WEEKDAY" => 1),
    "Tuesday" =>    array(  "OT" => $tuesdayOT,
                            "LIEU" => $tuesdayLIEU,
                            "TOTAL" => $tuesdayOT+$tuesdayLIEU,
                            "STAT" => $tuesdaySTAT,
                            "WEEKDAY" => 1),
    "Wednesday" =>  array(  "OT" => $wednesdayOT,
                            "LIEU" => $wednesdayLIEU,
                            "TOTAL" => $wednesdayOT+$wednesdayLIEU,
                            "STAT" => $wednesdaySTAT,
                            "WEEKDAY" => 1),
    "Thursday" =>   array(  "OT" => $thursdayOT,
                            "LIEU" => $thursdayLIEU,
                            "TOTAL" => $thursdayOT+$thursdayLIEU,
                            "STAT" => $thursdaySTAT,
                            "WEEKDAY" => 1),
    "Friday" =>     array(  "OT" => $fridayOT,
                            "LIEU" => $fridayLIEU,
                            "TOTAL" => $fridayOT+$fridayLIEU,
                            "STAT" => $fridaySTAT,
                            "WEEKDAY" => 1),
    "Saturday" =>   array(  "OT" => $saturdayOT,
                            "LIEU" => $saturdayLIEU,
                            "TOTAL" => $saturdayOT+$saturdayLIEU,
                            "STAT" => $saturdaySTAT,
                            "WEEKDAY" => 0)
    );

Here's my code for output:

echo "<pre>" ;
echo "Day \t OT \t LIEU \t TOTAL \t STAT \t WEEKDAY";
array_map(function ($var) {
    echo "
", $weekly_array[0], "\t", $var['OT'], "\t", $var['LIEU'], "\t", $var['TOTAL'], "\t", $var['STAT'], "\t", $var['WEEKDAY'];
}, $weekly_array);
echo "</pre>";

Everything works but I can't get the week days to show (Sunday, Monday, etc...) - I tried $weekly_array[0] to display Sunday then Monday and so on for each line but it gives error: Notice: Undefined variable: weekly_array

Anyone know what i'm doing wrong ??

  • 写回答

2条回答 默认 最新

  • dongzan1970 2014-09-12 16:26
    关注

    Why not just use a standard foreach loop?

    echo "<pre>" ;
    echo "Day \t OT \t LIEU \t TOTAL \t STAT \t WEEKDAY";
    foreach ($weekly_array as $key => $var) {
        echo "
    " .
        $key . "\t".
        $var['OT'] . "\t".
        $var['LIEU'] . "\t".
        $var['TOTAL'] . "\t".
        $var['STAT'] . "\t".
        $var['WEEKDAY'];
    }
    echo "</pre>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致