quick_learner42
quick_learner42

Reputation: 416

Json Response array loop

I have read pages and pages and i'm lost on how to proceed. I am trying to extract the price from a json array but the data I'm being provided seems to require i have the price first?

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'];
    }
}

a partial snippet of the actual json:

{"symbol":"GLD","status":"SUCCESS","underlying":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","symbol":"GLD_112318C115.5","description":"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":null,"tradeTimeInLong":1542995987379,"quoteTimeInLong":1542995999544,"netChange":-0.31,"volatility":-999.0,"delta":1.0,"gamma":0.0,"theta":-0.008,"vega":0.0,"rho":0.003,"openInterest":2231,"timeValue":-0.05,"theoreticalOptionValue":-999.0,"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","symbol":"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,"volatility":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,"multiplier":100.0,"settlementType":" ","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","symbol":"GLD_113018C115.5","description":"GLD Nov 30 2018 115.5 Call (Weekly)","exchangeName":"OPR","bid":0.77,"ask":0.79,"last":0.76,"mark":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,"volatility":9.003,"delta":0.582,"gamma":0.253,"theta":-0.042,"vega":0.067,"rho":0.015,"openInterest":755,"timeValue":0.49,"theoreticalOptionValue":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 Nov 30 2018 116 Call (Weekly)","exchangeName":"OPR","bid":0.52,"ask":0.55,"last":0.53,"mark":0.54,"bidSize":920,"askSize":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,"volatility":9.165,"delta":0.454,"gamma":0.252,"theta":-0.043,"vega":0.068,"rho":0.011,"openInterest":2948,"timeValue":0.53,"theoreticalOptionValue":0.535,"theoreticalVolatility":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}]},"2018-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,"highPrice":1.16,"lowPrice":1.07,"openPrice":0.0,"closePrice":1.27,"totalVolume":3,"tradeDate":null,"tradeTimeInLong":1542988161634,"quoteTimeInLong":1542995999922,"netChange":-0.2,"volatility":9.532,"delta":0.569,"gamma":0.176,"theta":-0.034,"vega":0.092,"rho":0.027,"openInterest":214,"timeValue":0.8,"theoreticalOptionValue":1.085,"theoreticalVolatility":29.0,"optionDeliverablesList":null,"strikePrice":115.5,"expirationDate":1544238000000,"d

Upvotes: 0

Views: 127

Answers (2)

ryantxr
ryantxr

Reputation: 4219

Looking at the data, the following code should work.

$data = json_decode($jsonString, true);
$history = $data['callExpDateMap'];
foreach( $history as $date => $entry ) {
    foreach ( $entry as $key => $row ) {
        $strikePrice = $row['strikePrice'];
        echo "Date : " . $date . "\n";
        echo "Strike price : " . $strikePrice . "\n";
    }
}

Upvotes: 0

Nick Dawes
Nick Dawes

Reputation: 2244

There is no "Price" key in the JSON you've supplied. However, I can see "symbol", "status", "volatility" etc. If Price is on the same level as these keys, you would access this way:

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

If Price were a level deeper, you'd access via the first level key this way:

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

And so on. There are a few ways to visualise the JSON, try pasting it here https://jsonformatter.curiousconcept.com/ and you should be able to easily find where "Price" lives, but I suspect it's a top level key.

Upvotes: 1

Related Questions