在highchart系列中使用的复杂多维数组

时间:2017-07-07 15:05:12

标签: php arrays multidimensional-array highcharts yii2

我需要一些帮助来创建/修改多维数组。我从yii2 ArrayDataProvider创建了这个数组:

Array(
[0] => Array
    (
        [resumo_mes] => 2017-06-01
        [total_total] => 15826
        [total_chegadas] => 9560
        [total_partidas] => 6266
        [total_36h] => 9633
        [chegadas_36h] => 6076
        [partidas_36h] => 3557
        [total_90m_36h] => 5510
        [chegadas_90m_36h] => 3001
        [partidas_90m_36h] => 2509
        [total_90m] => 683
        [chegadas_90m] => 483
        [partidas_90m] => 200
    )

[1] => Array
    (
        [resumo_mes] => 2017-05-01
        [total_total] => 16170
        [total_chegadas] => 9625
        [total_partidas] => 6545
        [total_36h] => 9962
        [chegadas_36h] => 6057
        [partidas_36h] => 3905
        [total_90m_36h] => 5542
        [chegadas_90m_36h] => 3139
        [partidas_90m_36h] => 2403
        [total_90m] => 666
        [chegadas_90m] => 429
        [partidas_90m] => 237
    )
)

我正在努力实现这个目标:

Array(
[0] => Array
    (
        [type] => column
        [name] => 2017-06-01
        [data] => Array
            (
                [0] => 15826
                [1] => 9560
                [2] => 6266
                [3] => 9633
                [4] => 6076
                [5] => 3557
                [6] => 5510
                [7] => 3001
                [8] => 2509
                [9] => 683
                [10] => 483
                [11] => 200
            )

    )

[1] => Array
    (
        [type] => column
        [name] => 2017-05-01
        [data] => Array
            (
                [0] => 16170
                [1] => 9625
                [2] => 6545
                [3] => 9962
                [4] => 6057
                [5] => 3905
                [6] => 5542
                [7] => 3139
                [8] => 2403
                [9] => 666
                [10] => 429
                [11] => 237
            )

    )
)

但是我很难获得这些代码:

$data = $dataProvider->getModels();
foreach ($data as $d) {
    $mes[] = $d['resumo_mes'];
    $total[] = intval($d['total_total']);
};

foreach ($mes as $m) {
    $series[] = array(
            'type' =>'column', 'name' =>$m, 
            'data' => $total
        );
};

我能够做到这一点:

Array(
[0] => Array
    (
        [type] => column
        [name] => 2017-06-01
        [data] => Array
            (
                [0] => 15826
                [1] => 16170
            )

    )

[1] => Array
    (
        [type] => column
        [name] => 2017-05-01
        [data] => Array
            (
                [0] => 15826
                [1] => 16170
            )

    )

)

正如您所看到的,值来自两个内部数组的total_total。如何像上面给出的例子那样设法拆分它们?

1 个答案:

答案 0 :(得分:1)

循环并构建结果,在使用后取消设置resumo_mes。然后得到剩余的值:

foreach($data as $key => $values) {
    $series[$key]['type'] = 'column';
    $series[$key]['name'] = $values['resumo_mes'];
    unset($values['resumo_mes']);
    $series[$key]['data'] = array_map('intval', array_values($values));
}

您还可以使用is_numericis_int过滤值,具体取决于类型。 is_numeric适用于您的样本数据,因为日期不是“数字”:

foreach($data as $values) {
    $series[] = array('type' => 'column',
                      'name' => $values['resumo_mes'],
                      'data' => array_map('intval',
                                          array_values(
                                          array_filter($values,
                                                       'is_numeric'))));
}

使用array_map添加了intval