简体   繁体   English

PHP超复杂关联数组解析为HTML表格式

[英]PHP Super Complicated Associative Array Parsing to HTML Table Format

I am trying to figure out how to parse an associative array I have constructed from database data in such a way that I can produce the html table layout that I'm looking for. 我试图弄清楚如何解析从数据库数据构造的关联数组,从而可以产生所需的html表布局。

Associative array structure is as follows: 关联数组的结构如下:

Array
(
  [2013] => Array
    (
        [1] => Array
            (
                [Total] => Array
                    (
                        [validleads] => 7327
                        [conversions] => 9856
                        [bookings] => 2449
                    )
                    ... more types for this week
                 [PPC] => Array
                    (
                        [validleads] => 884
                        [conversions] => 1109
                        [bookings] => 605
                    )
            )
          .. more weeks for 2013
    )
  [2012] => Array
    (
        [1] => Array
            (
                [Total] => Array
                    (
                        [validleads] => 9423
                        [conversions] => 12459
                        [bookings] => 2629
                    )
                [PPC] => Array
                    (
                        [validleads] => 955
                        [conversions] => 1237
                        [bookings] => 601
                    )
    )
)

So, there is one global array ( $data ) which contains an array for each year and in the arrays for each year there is an array for each week of the year which in turn has an array of figures for that week plus a totals array. 因此,有一个全局数组( $data ),其中包含每年的数组,并且在每年的数组中,每年的每个星期都有一个数组,进而又有该周的数字数组以及总计数组。

Table structure ideally would be 理想的表结构是

| Week | Total 2013 | Total 2012 | PPC 2013 | % PPC 2013  |  PPC 2012  | % PPC 2012 |  Other types 2013  | % Other types 2013 | Other types 2012 | % Other types 2012  
|  01  |    7327    |    9423    |   884    |   12.06%    |    955     |    10.13%  |                    |                    |                  |                              

The percentages are just worked out by taking the figure for the that week in the year and dividing it by the total for that week of the year multiplied by 100. 百分比是通过将一年中该周的数字除以一年中该周的总数再乘以100得出的。

The table layout I want to produce is to generate one table row per week of the year with the total for that week in each year aswell as the total for each type in that week each year and the percentage this total is for the overall total of the week for that year.... 我要生成的表布局是每年一年中的每一周生成一个表行,其中包含该年中该周的总数以及每年该周中每种类型的总数,该总数占百分比的总数那年的星期...

Sorry if that's complicated.... 抱歉,那很复杂。

Problem is I just can't think how to produce what I want in HTML table format. 问题是我只是想不出如何以HTML表格格式产生想要的内容。

I know some foreach 's are required but that's as far as I get to. 我知道一些foreach是必需的,但据我所知。

Something like this you'd need a little work basically you'll have to lay out your table a little differently for it to work/look right but should be doable. 像这样的事情,您基本上需要做一些工作,您必须将表的布局有所不同才能使其工作/看起来正确,但应该是可行的。 Code below isn't perfect but hopefully will help you get your head around the problem. 下面的代码并不完美,但希望能帮助您解决问题。

for ($i=0;$i<count($data);$i++) {
     //initial loop for year
    echo "Year = ".$data[$i]."<br />";
     for ($z=0;$z<count($data[$i]);$z++) {
     //secondary loop for year
     echo "Week =".$data[$i][$z]."<br />";
     //no need to loop through final array if its always jsut total and ppc if its variable on the final loop another for will be needed though
    echo "Totals: ".$data[$i][$z]['Total']['validleads']." etc etc";
    echo "Totals: ".$data[$i][$z]['PPC']['validleads']." etc etc";

    }
    }

I tried to print your array using multiple foreach it doesnot do what you ask but it will give you some Idea how to use foreach to parse the array please have a look 我尝试使用多个foreach打印您的数组,它不能满足您的要求,但是会为您提供一些有关如何使用foreach解析数组的想法,请看一下

foreach($complexArray as $k =>$v) { echo $k."=>"; foreach($v as $kx=> $vx) { echo $kx."=> "; foreach($vx as $ky =>$vy) { echo $ky.":=> "; foreach($vy as $kz =>$vz) { echo $kz."=>".$vz."<br>"; } }

        }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM