duanrenchuo9244 2016-08-04 20:45
浏览 43
已采纳

PHP - 从json保存对象密钥

Sorry for the noobie question, I am trying to save some json data on to my database. The problem is, i don't know how to get any of the "parent_names". The "parent_name" is always the same as the child "name".

price-data.json

[
    {
        "Flag": {
            "name": "Flag",
            "safe_price": "118.31",
            "safe_net_price": "110.60",
            "total_volume": 3148,
            "7_days": {
                "median_price": "118.31",
                "lowest_price": "100.00",
                "highest_price": "132.25",
                "volume": 94
            }
        },
        "Pole": {
            "name": "Pole",
            "safe_price": "81.21",
            "safe_net_price": "70.62",
            "total_volume": 1,
            "7_days": {
                "volume": 0
            }
        },
        "Net": {
            "name": "Net",
            "safe_price": "0.89",
            "safe_net_price": "0.84",
            "total_volume": 763,
            "7_days": {
                "median_price": "0.89",
                "lowest_price": "0.65",
                "highest_price": "1.08",
                "volume": 30
            }
        }
    }
]

php

$filename = "price-data.json";  
$data = file_get_contents($filename);  
$array = json_decode($data, true);


foreach($array as $row)  
 {  
      $sql = "INSERT INTO table_all_prices(parent_name, name, safe_price, safe_net_price, total_volume, median_price, lowest_price, highest_price, volume) VALUES (
      '".$row["parent_name"]."', 
      '".$row["parent_name"]["name"]."', 
      '".$row["parent_name"]["safe_price"]."', 
      '".$row["parent_name"]["safe_net_price"]."', 
      '".$row["parent_name"]["total_volume"]."', 
      '".$row["parent_name"]["7_days"]["median_price"]."',
      '".$row["parent_name"]["7_days"]["lowest_price"]."',
      '".$row["parent_name"]["7_days"]["highest_price"]."',
      '".$row["parent_name"]["7_days"]["volume"]."'
      )";       
 }  
 echo "All Prices inserted to database";  
  • 写回答

1条回答 默认 最新

  • douyoufan7881 2016-08-04 21:09
    关注

    You need to access the key from the current array level in the loop, you can do that by

     foreach($array as $parent_name => $row)
    

    Adding a variable in before the $row variable think of this like array access

     array( 'key' => 'value' )
    

    Same deal. Now because you are in the "parent_name" level of the array you don't need to put the additional access key in there. So for this

    $row["parent_name"]["7_days"]["volume"]
    

    You can just do

    $row["7_days"]["volume"]
    

    Because you will be working in this part of the json

            "name": "Net",
            "safe_price": "0.89",
            "safe_net_price": "0.84",
            "total_volume": 763,
            "7_days": {
                "median_price": "0.89",
                "lowest_price": "0.65",
                "highest_price": "1.08",
                "volume": 30
            }
    

    Then because you have an outer array wrapper on your json [{ .. }] those square brackets. you may need to do $array[0] or $array = reset($array). Reset is probably better ( if that turns out to be the case ) because you will avoid some warning messages from PHP if the array is empty. Essentially, trying to access a key of 0 will give you an undefined index warning if it's empty..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式