简体   繁体   中英

Parsing JSON with PHP multiple arrays to MYSQL table

stackoverflow newbie here. I've been reading through similar articles trying to piece together the code that I'm after. Fairly new to PHP and JSON, so here goes.

I'm trying to split some JSON data with the end goal of storing the data as two separate columns in a MySQL table. The JSON data looks like the below -

{
"request": {
    "command": "series",
    "series_id": "PET.RBRTE.A"
},
"series": [{
    "series_id": "PET.RBRTE.A",
    "name": "Europe Brent Spot Price FOB, Annual",
    "units": "Dollars per Barrel",
    "f": "A",
    "unitsshort": "$\/bbl",
    "description": "Europe Brent Spot Price FOB",
    "copyright": "Thomson-Reuters",
    "source": "Thomson-Reuters",
    "start": "1987",
    "end": "2015",
    "updated": "2016-05-16T16:56:05-0400",
    "data": [
        ["2015", 52.32],
        ["2014", 98.97],
        ["2013", 108.56],
        ...
    ]
}]

This JSON data is available direct via a URL so I'm using $json = file_get_contents and $array = json_decode($json, true) to get the data initially into an array.

Dumping the array var_dump($array) shows it as the following -

Array
(

[request] => Array
    (
        [command] => series
        [series_id] => PET.RBRTE.A
    )

[series] => Array
    (
        [0] => Array
            (
                [series_id] => PET.RBRTE.A
                [name] => Europe Brent Spot Price FOB, Annual
                [units] => Dollars per Barrel
                [f] => A
                [unitsshort] => $/bbl
                [description] => Europe Brent Spot Price FOB
                [copyright] => Thomson-Reuters
                [source] => Thomson-Reuters
                [start] => 1987
                [end] => 2015
                [updated] => 2016-05-16T16:56:05-0400
                [data] => Array
                    (
                        [0] => Array
                            (
                                [0] => 2015
                                [1] => 52.32
                            )

                        [1] => Array
                            (
                                [0] => 2014
                                [1] => 98.97
                            )

                        [2] => Array
                            (
                                [0] => 2013
                                [1] => 108.56
                            )
                        [3] ...
                    )

            )

    )

All good so far. I then wanted to obtain the sub-array data with the data array series, of which there are 29 in this particular JSON dataset. [0] within each sub-array is a date (year) and [1] is a value associated with that year.

The closest I've been able to get to this data is with the code below -

$array_data = implode(",", $array['series'][0]['data'][0]);

Which when using echo '<pre>'; print_r(($array_data)); echo '</pre>'; echo '<pre>'; print_r(($array_data)); echo '</pre>'; gives me 2015,52.32 , but what I'd like to get is the result for each of the sub-arrays as their own variable that I can then use to insert into MySQL, for example -

2015,52.32
2014,98.97
2013,108.56
...,... 

I would then use an insert statement to add the first value into a datetype column and the second value into a decimal column.

I feel I'm going about this the long way round, so if anyone has suggestions on how to improve this to get the end result, I would be most grateful. Thanks for the help.

Use a for loop:

$dataArray = $array['series'][0]['data'];
for ($i = 0; $i < count($dataArray); $i++) {
  $implosion = $dataArray[$i]);
}

If you want a dictionary:

$dataArray = $array['series'][0]['data'];
$dict = [];

for ($i = 0; $i < count($dataArray); $i++) {
  $dict[dataArray[$i][0]] = $dataArray[$i][1];
}

Giving:

Array
(
[2015] => 52.32
...
)

EDIT For separate loops:

$dataArray = $array['series'][0]['data'];
$dates = [];
$values = [];

for ($i = 0; $i < count($dataArray); $i++) {
  array_push($dates, $dataArray[$i][0]); 
  array_push($values, $dataArray[$i][1]);
}

$dateImplosion = implode(", ", $dates);
echo $dateImplosion;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM