doushi3715 2014-09-12 08: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 08: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
  • ¥20 谁刷目标页面的uv记录器上数据,数据只记录跳转的数值
  • ¥30 数据库软件的安装方法
  • ¥15 一道以太网数据传输题
  • ¥15 python 下载群辉文件
  • ¥50 代码还没怎么运行但是需要代码功能调用数据
  • ¥15 vue请求不到数据,返回状态200,数据为html
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找