RAAPereira
RAAPereira

Reputation: 49

Complicated multidimensional array to use in highchart series

I need some help creating/modifying a multidimensional array. I have this array created from 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
    )
)

I'm trying to achieve this:

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
            )

    )
)

But I'm stuck getting the values, with this code:

$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
        );
};

I was able to achieve this:

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
            )

    )

)

As you can see the values are from total_total of both inner arrays. How do I manage to split them like the example given above?

Upvotes: 2

Views: 527

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Loop and build your result, unsetting resumo_mes after you use it. Then get the remaining values:

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));
}

You could also filter the values with is_numeric or is_int depending on the type. is_numeric works for your sample data as the date is not "numeric":

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

Added array_map with intval.

Upvotes: 1

Related Questions