在PHP中使用for和foreach打印数据

时间:2015-03-09 11:45:49

标签: php html arrays laravel

Array(
    [2010] => Array
        (
            [Jan] => 437330815.99
            [Feb] => 494623486.71
            [Mar] => 624862935.96
            [Apr] => 540187633.38
            [May] => 568721136.13
            [Jun] => 590699632.90
            [Jul] => 607545468.15
            [Aug] => 570176086.77
            [Sep] => 531285761.85
            [Oct] => 578118109.69
            [Nov] => 491206839.11
            [Dec] => 431679990.82
        )

    [2011] => Array
        (
            [Jan] => 497468548.16
            [Feb] => 588439025.38
            [Mar] => 658012492.19
            [Apr] => 626096428.77
            [May] => 620109705.91
            [Jun] => 662820990.91
            [Jul] => 672935236.81
            [Aug] => 642683423.52
            [Sep] => 617806834.00
            [Oct] => 652805181.88
            [Nov] => 562568021.26
            [Dec] => 479345363.92
        )

    [2012] => Array
        (
            [Jan] => 548317281.33
            [Feb] => 615341614.56
            [Mar] => 769556028.41
            [Apr] => 692880692.03
            [May] => 711399744.44
            [Jun] => 757158917.80
            [Jul] => 745507204.09
            [Aug] => 725701414.42
            [Sep] => 639134772.40
            [Oct] => 728571248.14
            [Nov] => 628597962.27
            [Dec] => 530560426.26
        )

    [2013] => Array
        (
            [Jan] => 608651115.68
            [Feb] => 673980376.31
            [Mar] => 820412769.66
            [Apr] => 781953854.20
            [May] => 776503249.83
            [Jun] => 785424092.78
            [Jul] => 812738011.28
            [Aug] => 736895678.95
            [Sep] => 673957777.36
            [Oct] => 768675960.16
            [Nov] => 689766955.00
            [Dec] => 576145057.68
        )

[2014] => Array
    (
        [Jan] => 654355925.30
        [Feb] => 718191945.21
        [Mar] => 891949985.80
        [Apr] => 818734317.36
        [May] => 856014090.02
        [Jun] => 862265933.29
        [Jul] => 890071168.00
        [Aug] => 816479581.34
        [Sep] => 755628938.61
        [Oct] => 883456190.41
    )
)

我有这样的打印数据,我可以获取,我打印了$revenue_data变量。

这是我的查看文件代码。

我需要这张表,请点击链接http://laravel.io/bin/VPrOP

但是我的数据打印得像这样,请点击http://laravel.io/bin/LkGeO

我的观看文件代码在此处请点击此链接laravel.io/bin/wJYMe#

请帮我打印正确的东西。

提前致谢。

2 个答案:

答案 0 :(得分:1)

首先,您可以更改数据的结构:

$newArray = array(array());
foreach ($originalArray as $year => $yearInfo) {
   $newArray['Month'][$year] = $year;  // prepare firt line
   foreach($yearInfo as $moth => $info) {
      $newArray[$month][$year] = $info; // prepares the rest of the info to show
   }
}

然后,如果您使用的是laravel刀片文件:

@foreach ($newArray as $month=>$data)
{{ $month }}
@foreach ($data as $info)
{{ $info }}
@endforeach
<br />
@endforeach

答案 1 :(得分:0)

$month = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

for ($i = 0; $i < sizeof($month); $i++) { 

    for ($j = 2010; $j < 2015; $j++) { 

        echo $YourArray[$j][$month[$i]];
        echo "\t";
    }

    echo "<br>";
}