我正在尝试使用php创建具有多维数组的html表,

时间:2019-11-05 16:21:49

标签: php multidimensional-array foreach

我的数组开始像这样

Array
(
    [report] => Cost History
    [dimensions] => Array
        (
            [0] => Array
                (
                    [time] => Array
                        (
                            [0] => Array
                                (
                                    [label] => Total
                                    [name] => total
                                )

                            [1] => Array
                                (
                                    [name] => 2018-11
                                    [label] => 2018-11
                                    [direct] => 
                                    [populated] => 1
                                    [excluded] => 
                                    [extended] => 
                                    [sort_order] => 
                                    [parent] => -1
                                )

                            [2] => Array
                                (
                                    [name] => 2018-12
                                    [label] => 2018-12
                                    [direct] => 
                                    [populated] => 1
                                    [excluded] => 
                                    [extended] => 
                                    [sort_order] => 
                                    [parent] => -1
                                )

[时间] [0]-> [13]创建标题。我要在最后一栏。 接下来,我有

            [1] => Array
                (
                    [Category] => Array
                        (
                            [0] => Array
                                (
                                    [label] => Total
                                    [name] => total
                                )

                            [1] => Array
                                (
                                    [name] => sqs
                                    [label] => Simple Service
                                    [direct] => 
                                    [populated] => 
                                    [excluded] => 
                                    [extended] => 
                                    [sort_order] => 
                                    [parent] => -1
                                )


这与时间处于同一水平,并且需要在底部是我的第一列,共有[0]-> [281]行。 接下来我有数据。

    [data] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => 59639.827459504
                        )

                    [1] => Array
                        (
                            [0] => 0.4566711208
                        )

                    [2] => Array
                        (
                            [0] => 
                        )

                    [3] => Array
                        (
                            [0] => 
                        )

                    [4] => Array
                        (
                            [0] => 39.2084171093
                        )

此[数据]处于[维度]级别 数组的第一级用于1-> [13]列。第二层是1-> [281]行。第一行在底部,total,[data]第二个数组total [0]在每一行的末尾。因此右下角为$ 59639.82。

下一步,我需要删除总计= 0的所有行。

这是我从头开始的内容,它为我提供了第一行和第一列。

$json = json_decode($result, true);
$header        = $json['dimensions'][0]['time'];
$file = fopen('file/test.html', 'w');
$header1 = array();
$header1[0] = '<h2>Combined cost report ACR AMA</h2><table border="3">' . PHP_EOL . '<tr>' . PHP_EOL .  '<th>Services</th>' . PHP_EOL;
foreach($header as $item) {
  $header1[] = '<th>' .  $item['label'] . '</th>' . PHP_EOL;
}
$v = $header1[1];
unset($header1[1]);
$header1[] = $v;
$header1[] = '</tr>' . PHP_EOL;
#$header1 = array_reverse($header1);
$header2 = implode("",$header1);
fwrite($file, $header2);
$service = array();
$service1 = array();
$services = $json['dimensions'][1]['Category'];
foreach($services as $item) {
  $service[] = '<tr>';
  $service[] = '<td>' . $item['label'] . '</td>';
  $service[] = '</tr>';
}
$v = $service[0];
unset($service[0]);
$service[] = $v;
$service = implode("",$service);
fwrite($file, $service);
$close = '</table>';
fwrite($file, $close);
fclose($file);

What I have

What I want

0 个答案:

没有答案