简体   繁体   English

使用PHP多个数组将JSON解析为MYSQL表

[英]Parsing JSON with PHP multiple arrays to MYSQL table

stackoverflow newbie here. 这里的stackoverflow新手。 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. 对PHP和JSON来说还算是新手,所以去吧。

I'm trying to split some JSON data with the end goal of storing the data as two separate columns in a MySQL table. 我试图拆分一些JSON数据,最终目的是将数据存储为MySQL表中的两个单独的列。 The JSON data looks like the below - JSON数据如下所示-

{
"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. 可以通过URL直接获得此JSON数据,因此我正在使用$json = file_get_contents$array = json_decode($json, true)将数据最初放入数组。

Dumping the array var_dump($array) shows it as the following - 转储数组var_dump($array)显示如下:

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. 然后,我想使用data数组系列获取子数组数据,在此特定的JSON数据集中有29个。 [0] within each sub-array is a date (year) and [1] is a value associated with that year. 每个子数组中的[0]是日期(年),[1]是与该年关联的值。

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>'; 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 ,但是我想要得到的是每个子数组的结果作为它们自己的变量,然后我可以使用它们插入到MySQL中,例如-

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. 然后,我将使用插入语句将第一个值添加到datetype列中,然后将第二个值添加到小数列中。

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: 使用for循环:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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