douweiluo0600 2016-08-09 20:46
浏览 63
已采纳

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

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>'; 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.

  • 写回答

1条回答 默认 最新

  • douxie1894 2016-08-09 20:50
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题