Skip to content
Advertisement

Loop trough and modify JSON response

I am making API request to Google Sheets and I receive this response:

Array
(
    [0] => Array
        (
            [Title] => Hours
            [January] => 1
            [February] => 2
            [March] => 3
            [April] => 4
            [May] => 5
            [June] => 6
            [July] => 7
            [August] => 8
        )

    [1] => Array
        (
            [Title] => Days
            [January] => 3
            [February] => 5
            [March] => 1
            [April] => 6
            [May] => 3
            [June] => 7
            [July] => 4
            [August] => 2
        )

    [2] => Array
        (
            [Title] => Weeks
            [January] => 3
            [February] => 5
            [March] => 3
            [April] => 4
            [May] => 0
            [June] => 0
            [July] => 2
            [August] => 6
            [September] => 0
            [October] => 0
            [November] => 1
            [December] => 0
        )

)

How could I loop trough and modify this array to something like this so I can use it with HighCharts JS library?

series: [{
        title: 'Hours',
        data: [1, 2, 3, 4, 5, 6 .......]

    }, {
        title: 'Days',
        data: [4, 6, 3, 6, ........]

    }, {
        title: 'Weeks',
        data: [1, 9, 1, 3, ........]

    }, {

       ....

    }]

I tried this way:

if ($response->status) {
            $rawData = json_decode(json_encode($response->data), true);
        }

        $series = [];

        foreach ($rawData as $index => $rawDatum) {

            if (!isset($rawDatum['Title'])) {
                continue;
            }

            foreach ($rawDatum as $columnKey => $value) {

                if ($columnKey == 'CvA') {
                    $series[$columnKey]['Title'][] = $value;
                }

                

            }
            
        }

What I got as result:

Array
(
    [Title] => Array
        (
            [title] => Array
                (
                    [0] => Hours
                    [1] => Days
                    [2] => Weeks
                )

        )

)

Also is there a way to get all names of the months saved in $months array for example without doubles?

Advertisement

Answer

The following piece of code will create an array with title and the values for each respective subarray Hours, Days, Weeks.

We will also collect the union of the months in an array named $months.

$series = [];
$months = [];

foreach ($rawData as $subarray) {
    $title = $subarray['Title'];
    // Remove title key
    unset($subarray['Title']);

    $series[] = [
        'title' => $title,
        'data' => array_values($subarray),
    ];

    // Union operator to keep unique months.
    $months += array_keys($subarray);
}

echo '<pre>';
print_r($months);
echo '</pre>';

echo '<pre>';
print_r($result);
echo '</pre>';

Result $months:

Array
(
    [0] => January
    [1] => February
    [2] => March
    [3] => April
    [4] => May
    [5] => June
    [6] => July
    [7] => August
    [8] => September
    [9] => October
    [10] => November
    [11] => December
)

Result $series:

Array
(
    [0] => Array
        (
            [title] => Hours
            [data] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                    [4] => 5
                    [5] => 6
                    [6] => 7
                    [7] => 8
                )

        )

    [1] => Array
        (
            [title] => Days
            [data] => Array
                (
                    [0] => 3
                    [1] => 5
                    [2] => 1
                    [3] => 6
                    [4] => 3
                    [5] => 7
                    [6] => 4
                    [7] => 2
                )

        )

    [2] => Array
        (
            [title] => Weeks
            [data] => Array
                (
                    [0] => 3
                    [1] => 5
                    [2] => 3
                    [3] => 4
                    [4] => 0
                    [5] => 0
                    [6] => 2
                    [7] => 6
                    [8] => 0
                    [9] => 0
                    [10] => 1
                    [11] => 0
                )

        )

)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement