[英]PHP - Pull data from JSON
我有一個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},
{"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'];
迭代$data
數組,並將數組中新元素的鍵設置為tag
元素,並將其值設置為value
元素。
foreach($data['data'] as $v)
$array[$v['tag']] = $v['value'];
$array
包含您想要的內容,像這樣訪問: $array['operatingRevenue']
迭代$ data並使用array_push。 在這里閱讀。 https://www.w3schools.com/php/func_array_push.asp
$data = json_decode($jsondata, true);
$datas = [];
foreach($data['data'] as $val){
array_push($datas,["tag"=>$val['tag'],"value"=>$val['tag']])
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.