使用动态标头和数据

时间:2017-10-13 10:29:41

标签: php

我正在尝试从数据库中检索的数组创建一个表。

数组是多维的,动态的。

我的示例数组是

Array
(
[0] => Array
    (
        [licensee] => AmazonUK
        [totalCounts] => 2
        [netRoyalty] => 0.01006506
        [month] => 2017-05-01
    )

[1] => Array
    (
        [licensee] => AppleMusicGB
        [totalCounts] => 52
        [netRoyalty] => 43.39295886
        [month] => 2017-04-01
    )

[2] => Array
    (
        [licensee] => AppleMusicHK
        [totalCounts] => 2
        [netRoyalty] => 6.71799264
        [month] => 2017-05-01
    )

[3] => Array
    (
        [licensee] => AppleMusicIN
        [totalCounts] => 22
        [netRoyalty] => 1.29255252
        [month] => 2017-03-01
    )

[4] => Array
    (
        [licensee] => AppleMusicUS
        [totalCounts] => 192
        [netRoyalty] => 328.95685302
        [month] => 2017-03-01
    )

[5] => Array
    (
        [licensee] => Deezer
        [totalCounts] => 18
        [netRoyalty] => 2.91837036
        [month] => 2017-01-01
    )

[6] => Array
    (
        [licensee] => GoogleLocker
        [totalCounts] => 10
        [netRoyalty] => 0.08685774
        [month] => 2017-01-01
    )

[7] => Array
    (
        [licensee] => GoogleSubscription
        [totalCounts] => 30
        [netRoyalty] => 57.13748172
        [month] => 2017-03-01
    )

[8] => Array
    (
        [licensee] => Spotify
        [totalCounts] => 356
        [netRoyalty] => 179.53991898
        [month] => 2017-03-01
    )

[9] => Array
    (
        [licensee] => iTunesAU
        [totalCounts] => 14
        [netRoyalty] => 3.14949396
        [month] => 2017-02-01
    )

[10] => Array
    (
        [licensee] => iTunesCA
        [totalCounts] => 16
        [netRoyalty] => 0.18564444
        [month] => 2017-01-01
    )

[11] => Array
    (
        [licensee] => iTunesEU
        [totalCounts] => 4
        [netRoyalty] => 1.00737582
        [month] => 2017-02-01
    )

[12] => Array
    (
        [licensee] => iTunesGB
        [totalCounts] => 8
        [netRoyalty] => 0.91430508
        [month] => 2017-03-01
    )

[13] => Array
    (
        [licensee] => iTunesUS
        [totalCounts] => 2
        [netRoyalty] => 99.69851988
        [month] => 2017-06-01
    )

)

我想创建一个像:

的表

被许可人为水平标题

月垂直标题

totalCounts和netRoyalty为各自被许可人的totalCounts / netRoyalty

包含样本表的jsfiddle。

jsfiddle

编辑:

我试过的是

<table id="table1" class="table table-striped table-hover table-fw-widget">
                          <thead>
                            <tr>
                              <?php
                                foreach($distributorReportAnalysis as $distreportkey => $distreportval){
                              ?>
                                <th><?php echo $distreportval[licensee]; ?></th>
                              <?php
                                }
                              ?>
                            </tr>
                          </thead>

                          <tbody>
                          <?php
                            foreach($distributorReportAnalysis as $distreportk => $distreportv){
                          ?>
                            <tr class="odd gradeX">
                              <th><?php echo date('M Y',strtotime($distreportv[month])); ?></th>
                              <?php
                                foreach($distreportv as $reportk => $reportv){
                              ?>

                              <td><?php echo $distreportv['totalCounts']."/".round($distreportv['netRoyalty']); ?></td>
                              <?php 
                                }
                              ?>
                            </tr>
                          <?php 
                            }
                          ?>
                          </tbody>
                        </table>

1 个答案:

答案 0 :(得分:0)

这是一个如何使用array_map提取值的快速示例。实际上foreach也可以工作,如果你想修改原始数组,array_map特别有用。 所以这里的想法是在显示之前组织数据,这里分别按月获取标题和值。对于你的显示器,你仍然必须将好的值放在数组中的好位置,但它现在按标题值排序,这样很容易。

<?php
    $arr = [
        0 => [
            'licensee' => 'AmazonUK',
            'totalCounts' => '2',
            'netRoyalty' => '0.01006506',
            'month' => '2017-05-01',
        ],

        1 => [
            'licensee' => 'AppleMusicGB',
            'totalCounts' => '52',
            'netRoyalty' => '43.39295886',
            'month' => '2017-04-01',
        ],
    ]; 

    $headers = [];
    $rows = [];

    function customExtract($element){
        global $headers, $rows;

        //an array for your headers
        if(!in_array($element['licensee'], $headers)){
            $headers[] = $element['licensee'];
        }

        //another for the rows by month
        if(!isset($rows[$element['month']])){
            $rows[$element['month']] = [];
        }
        $rows[$element['month']][$element['licensee']] = $element['totalCounts'] . '/' . $element['netRoyalty'];

        //you can also transform the original array element or not (if not you just return it)
        return $element;
    }

    $arr = array_map('customExtract', $arr);

    var_dump($headers);
    echo'<br />';
    var_dump($rows);
?>

结果:

array(2) { [0]=> string(8) "AmazonUK" [1]=> string(12) "AppleMusicGB" }
array(2) { ["2017-05-01"]=> array(1) { ["AmazonUK"]=> string(12) "2/0.01006506" } ["2017-04-01"]=> array(1) { ["AppleMusicGB"]=> string(14) "52/43.39295886" } }