douyi1341
2017-06-08 00:06
浏览 259
已采纳

PHP - 从JSON中提取数据

I have a JSON file where I'm struggling getting the data out. Here is the JSON.

{"data":[
        {"tag":"operatingrevenue","value":215639000000.0},
        {"tag":"totalrevenue","value":215639000000.0},
        {"tag":"operatingcostofrevenue","value":131376000000.0},
        {"tag":"totalcostofrevenue","value":131376000000.0},
        {"tag":"totalgrossprofit","value":84263000000.0},
        {"tag":"sgaexpense","value":14194000000.0},
        {"tag":"rdexpense","value":10045000000.0},
        {"tag":"totaloperatingexpenses","value":24239000000.0},
        {"tag":"totaloperatingincome","value":60024000000.0},
        {"tag":"otherincome","value":1348000000.0},
        {"tag":"totalotherincome","value":1348000000.0},
        {"tag":"totalpretaxincome","value":61372000000.0},
        {"tag":"incometaxexpense","value":15685000000.0},
        {"tag":"netincomecontinuing","value":45687000000.0},
        {"tag":"netincome","value":45687000000.0},
        {"tag":"netincometocommon","value":45687000000.0},
        {"tag":"weightedavebasicsharesos","value":5470820000.0},
        {"tag":"basiceps","value":8.35},
        {"tag":"weightedavedilutedsharesos","value":5500281000.0},
        {"tag":"dilutedeps","value":8.31},
        {"tag":"weightedavebasicdilutedsharesos","value":5471500000.0},
        {"tag":"basicdilutedeps","value":8.35},
        {"tag":"cashdividendspershare","value":2.18}]

I'm converting it to an array using

$data = json_decode($jsondata, true);

I can then pull the data by:

$operatingRevenue = $data['data'][0]['value'];
$totalrevenue = $data['data'][1]['value'];

etc, however, I want to use the tag name and not just the order just in case the order changes in the JSON. Something like

$operatingRevenue = $data['data']['operatingRevenue'];

图片转代码服务由CSDN问答提供 功能建议

我有一个JSON文件,我正在努力获取数据。 这是JSON。

  {“data”:[
 {“tag”:“operatingrevenue”,“value”:215639000000.0},
 {“tag”:  “totalrevenue”,“value”:215639000000.0},
 {“tag”:“operatingcostofrevenue”,“value”:131376000000.0},
 {“tag”:“totalcostofrevenue”,“value”:131376000000.0},
 {  “tag”:“totalgrossprofit”,“value”:84263000000.0},
 {“tag”:“sgaexpense”,“value”:14194000000.0},
 {“tag”:“rdexpense”,“value”:10045000000.0}  ,
 {“tag”:“totaloperatingexpenses”,“value”:24239000000.0},
 {“tag”:“totaloperatingincome”,“value”:60024000000.0},
 {“tag”:“otherincome”,“value  “:1348000000.0},
 {”tag“:”totalotherincome“,”value“:1348000000.0},
 {”tag“:”totalpretaxincome“,”value“:61372000000.0},
 {”tag“:”incometaxexpense  “,”“value”:15685000000.0},
 {“tag”:“netincomecontinuing”,“value”:45687000000.0},
 {“tag”:“netincome”,“value”:45687000000.0},
 {“tag  “:”netincometocommon“,”value“:45687000000.0},
 {”tag  “:”weightedavebasicsharesos“,”value“:5470820000.0},
 {”tag“:”basiceps“,”value“:8.35},
 {”tag“:”weightedavedilutedsharesos“,”value“:5500281000.0},\  n {“tag”:“dilutedeps”,“value”:8.31},
 {“tag”:“weightedavebasicdilutedsharesos”,“value”:5471500000.0},
 {“tag”:“basicdilutedeps”,“value”:  8.35},
 {“tag”:“cashdividendspershare”,“value”:2.18}] 
   
 
 

我正在使用

  $ data = json_decode($ jsondata,true); 
   
 
 

然后我可以通过以下方式提取数据:

  $ operatingRevenue = $ data ['data'] [0] ['value']; 
 $ totalrevenue = $ data ['data'] [1] ['value']  ; 
   
 
 

等,但是,我希望使用标记名称而不仅仅是订单,以防订单在JSON中发生变化。 类似

  $ operatingRevenue = $ data ['data'] ['operatingRevenue']; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqq46733 2017-06-08 00:11
    已采纳

    Iterate the $data array, and set the key of a new element in the array to the tag element, and it's value to the value element.

    foreach($data['data'] as $v)
        $array[$v['tag']] = $v['value'];
    

    $array contains what you want, access like this: $array['operatingRevenue']

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题