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

时间:2014-09-12 16:12:25

标签: php

我正在尝试以漂亮的格式输出我的多数组,以便我可以轻松阅读

这是我的数组代码:

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

这是我的输出代码:

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

一切正常但我无法显示周日(星期日,星期一等等) - 我尝试$weekly_array[0]显示星期日然后星期一,依此类推每行,但它给出了{{ 1}}

任何人都知道我做错了什么?

2 个答案:

答案 0 :(得分:1)

为什么不使用标准foreach循环?

echo "<pre>" ;
echo "Day \t OT \t LIEU \t TOTAL \t STAT \t WEEKDAY";
foreach ($weekly_array as $key => $var) {
    echo "\n" .
    $key . "\t".
    $var['OT'] . "\t".
    $var['LIEU'] . "\t".
    $var['TOTAL'] . "\t".
    $var['STAT'] . "\t".
    $var['WEEKDAY'];
}
echo "</pre>";

答案 1 :(得分:0)

使用foreach循环迭代数组,然后使用html:

显示

您可以插入的示例。

foreach($weekly_array AS $value => $day) {
        echo "<strong>".$value."</strong><br>";
        foreach($day AS $key => $val){
            echo $key ." ".$val."</br>";
        }
       echo "<hr>";
    }