Json Response数组循环

时间:2018-11-23 21:32:42

标签: php arrays json

我已经阅读过许多页面,而我却迷失了如何继续。我正在尝试从json数组中提取价格,但是提供的数据似乎要求我先获取价格?

var_dump(json_decode($ resp,true)); $ Data = json_decode($ resp,true);

    foreach($Data as $jsonDataKey => $jsonDataValue){
        foreach($jsonDataValue as $jsonArrayKey => $jsonArrayValue){
            echo "<hr>Price :".$jsonArrayValue['Price'];
            echo " Date:".$jsonArrayValue['Date'];
        }
    }
array) {   ["status"]=> "SUCCESS"   ["Date"]=>   array(1) {
["2018-11-23"]=>
array() {
  ["115.5"]=>
  array() {
    [0]=>
    array(46) {
      ["Price"]=> float(115.5)
      ["Date"]=> int(1543028400000)
    }
  }
  ["116.0"]=>
  array() {
    [0]=>
    array() {
      ["Price"]=> float(116.0)
      ["Date"]=> int(1543028400000)
foreach($Data as $jsonDataKey => $jsonDataValue){
    foreach($jsonDataValue as $jsonArrayKey => $jsonArrayValue){
        echo "<hr>Price :".$jsonArrayValue['Price'];
        echo " Date:".$jsonArrayValue['Date'];
    }
}

实际json的部分片段:

  

{“ symbol”:“ GLD”,“ status”:“ SUCCESS”,“底层”:null,“ strategy”:“ SINGLE”,“ interval”:null,“ isDelayed”:true,“ isIndex”: false,“ interestRate”:2.64581,“ underlyingPrice”:115.56,“ volatility”:29.0,“ daysToExpiration”:0.0,“ callExpDateMap”:{“ 2018-11-23:0”:{“ 115.5”:[{“ putCall “:” CALL“,”符号“:” GLD_112318C115.5“,”描述“:” GLD Nov 23 2018 115.5 Call(Weekly)“,” exchangeName“:” OPR“,” bid“:0.17,” ask“: 0.28,“ last”:0.22,“ mark”:0.23,“ bidSize”:132,“ askSize”:20,“ lastSize”:0,“ highPrice”:0.38,“ lowPrice”:0.13,“ openPrice”:0.0, “ closePrice”:0.53,“ totalVolume”:351,“ tradeDate”:空,“ tradeTimeInLong”:1542995987379,“ quoteTimeInLong”:1542995999544,“ netChange”:-0.31,“波动率”:-999.0,“ delta”:1.0, “ gamma”:0.0,“ theta”:-0.008,“ vega”:0.0,“ rho”:0.003,“ openInterest”:2231,“ timeValue”:-0.05,“ theticalticalOptionValue”:-99.90,“ theoreticalVolatility”:29.0 ,“ optionDeliverablesList”:null,“ strikePrice”:115.5,“ expirationDate”:1543028400000,“ daysToExpiration”:0,“ expirationType”:“ S”,“ lastTradingDay”:1542949200000,“ multiplier”:100.0, “ settlementType”:“”,“ deliverableNote”:“”,“ isIndexOption”:null,“ percentChange”:-58.63,“ markChange”:-0.31,“ markPercentChange”:-57.69,“ inTheMoney”:true,“ nonStandard” :false,“ mini”:false}],“ 116.0”:[{“ putCall”:“ CALL”,“符号”:“ GLD_112318C116”,“ description”:“ GLD Nov 23 2018 116 Call(Weekly)”,“ exchangeName“:” OPR“,” bid“:0.0,” ask“:0.01,” last“:0.01,” mark“:0.01,” bidSize“:0,” askSize“:692,” lastSize“:0,” highPrice”:0.11,“ lowPrice”:0.01,“ openPrice”:0.0,“ closePrice”:0.26,“ totalVolume”:2167,“ tradeDate”:null,“ tradeTimeInLong”:1542995959548,“ quoteTimeInLong”:1542995984685,“ netChange” :-0.25,“波动率”:0.0,“ delta”:0.0,“ gamma”:0.0,“ theta”:0.0,“ vega”:0.0,“ rho”:0.0,“ openInterest”:2786,“ timeValue”: 0.01,“ theoreticalOptionValue”:0.0,“ theoreticalVolatility”:29.0,“ optionDeliverablesList”:null,“ strikePrice”:116.0,“ expirationDate”:1543028400000,“ daysToExpiration”:0,“ expirationType”:“ S”,“ lastTradingDay”: 1542949200000,“乘数”:100.0,“结算类型”:“”,“ deliverableNote”:“”,“ isIndexOption”:null,“ percentChange”:- 96.21,“ markChange”:-0.26,“ markPercentChange”:-98.1,“ inTheMoney”:false,“ nonStandard”:false,“ mini”:false}]}},“ 2018-11-30:7”:{“ 115.5 “:[{” putCall“:” CALL“,”符号“:” GLD_113018C115.5“,” description“:” GLD Nov 30 2018 115.5 Call(Weekly)“,” exchangeName“:” OPR“,” bid“: 0.77,“要求”:0.79,“最后”:0.76,“标记”:0.78,“ bidSize”:22,“ askSize”:18,“ lastSize”:0,“ highPrice”:0.89,“ lowPrice”:0.73, “ openPrice”:0.0,“ closePrice”:0.94,“ totalVolume”:217,“ tradeDate”:null,“ tradeTimeInLong”:1542995943992,“ quoteTimeInLong”:1542995999810,“ netChange”:-0.18,“波动率”:9.003,“ delta“:0.582,” gamma“:0.253,” theta“:-0.042,” vega“:0.067,” rho“:0.015,” openInterest“:755,” timeValue“:0.49,” theoryticalOptionValue“:0.78,” theoreticalVolatility “:29.0,” optionDeliverablesList“:null,” strikePrice“:115.5,” expirationDate“:1543633200000,” daysToExpiration“:7,” expirationType“:” S“,” lastTradingDay“:1543554000000,” multiplier“:100.0,” settlementType “:”“,” deliverableNote“:”“,” isIndexOption“:null,” percentChange“:-19.17,” markChange“:-0.16,” markPercentChange“:- 17.05,“ inTheMoney”:true,“ nonStandard”:false,“ mini”:false}],“ 116.0”:[{“ putCall”:“ CALL”,“ symbol”:“ GLD_113018C116”,“ description”:“ GLD 2018年11月30日116通话(每周)“,” exchangeName“:” OPR“,”出价“:0.52,”询问“:0.55,”最后“:0.53,”标记“:0.54,” bidSize“:920,”询问尺寸“:18,” lastSize“:0,” highPrice“:0.63,” lowPrice“:0.49,” openPrice“:0.0,” closePrice“:0.69,” totalVolume“:1278,” tradeDate“:null,” tradeTimeInLong“: 1542995969283,“ quoteTimeInLong”:1542995999922,“ netChange”:-0.16,“波动率”:9.165,“ delta”:0.454,​​“ gamma”:0.252,“ theta”:-0.043,“ vega”:0.068,“ rho”: 0.011,“ openInterest”:2948,“ timeValue”:0.53,“ theoryticalOptionValue”:0.535,“ theorytical Volatility”:29.0,“ optionDeliverablesList”:null,“ strikePrice”:116.0,“ expirationDate”:1543633200000,“ daysToExpiration”:7, “ expirationType”:“ S”,“ lastTradingDay”:1543554000000,“ multiplier”:100.0,“ settlementType”:“”,“ deliverableNote”:“”,“ isIndexOption”:null,“ percentChange”:-23.15,“ markChange” :-0.15,“ markPercentChange”:-22.43,“ inTheMoney”:false,“ nonStandard”:false,“ mini”:false}]},“ 2 018-12-07:14“:{” 115.5“:[{” putCall“:” CALL“,” symbol“:” GLD_120718C115.5“,” description“:” GLD Dec 7 2018 115.5 Call(Weekly)“, “ exchangeName”:“ OPR”,“ bid”:1.07,“ ask”:1.1,“ last”:1.07,“ mark”:1.09,“ bidSize”:496,“ askSize”:22,“ lastSize”:0, “高价”:1.16,“低价”:1.07,“开价”:0.0,“收盘价”:1.27,“总交易量”:3,“ tradeDate”:空,“ tradeTimeInLong”:1542988161634,“ quoteTimeInLong”:1542995999922,“ netChange” “:-0.2,”挥发性“:9.532,” delta“:0.569,” gamma“:0.176,” theta“:-0.034,” vega“:0.092,” rho“:0.027,” openInterest“:214,” timeValue “:0.8”,“理论期权价值”:1.085,“理论波动性”:29.0,“期权可交付物清单”:空,“行使价”:115.5,“到期日期”:1544238000000,“ d

2 个答案:

答案 0 :(得分:1)

您提供的JSON中没有“价格”键。但是,我可以看到“符号”,“状态”,“波动性”等。如果Price与这些键位于同一水平,您将通过以下方式访问:

foreach ($data as $value) {
    echo $value["Price"];
}

如果Price更深一层,您可以通过以下一级键进行访问:

foreach ($data as $value) {
    echo $value["firstLevel"]["Price"];
}

以此类推。有几种可视化JSON的方法,尝试将其粘贴在https://jsonformatter.curiousconcept.com/上,您应该可以轻松找到“ Price”的住处,但我怀疑它是顶级密钥。

答案 1 :(得分:0)

查看数据,以下代码应该起作用。

simple calculation