-1

I am new in PHP. I am trying to get some value from json file and following This Stackoverflow answer

When I run example given in answer working fine.

I have my.json file like below

{
  "quoteSummary": {
    "result": [
      {
        "summaryDetail": {
          "maxAge": 1,
          "priceHint": {
            "raw": 2,
            "fmt": "2",
            "longFmt": "2"
          },
          "previousClose": {
            "raw": 126,
            "fmt": "126.00"
          },
   
          "navPrice": {},
          "currency": "USD",
          "fromCurrency": null,
          "toCurrency": null,
          "lastMarket": null,
          "volume24Hr": {},
          "volumeAllCurrencies": {},
          "circulatingSupply": {},
          "algorithm": null,
          "maxSupply": {},
          "startDate": {},
          "tradeable": false
        },
        "defaultKeyStatistics": {
          "maxAge": 1,
          "priceHint": {
            "raw": 2,
            "fmt": "2",
            "longFmt": "2"
          },
 
          "lastDividendDate": {
            "raw": 1612483200,
            "fmt": "2021-02-05"
          },
          "lastCapGain": {},
          "annualHoldingsTurnover": {}
        },
        "earnings": {
          "maxAge": 86400,
          "earningsChart": {
            "quarterly": [
              {
                "date": "1Q2020",
                "actual": {
                  "raw": 0.64,
                  "fmt": "0.64"
                },
                "estimate": {
                  "raw": 0.56,
                  "fmt": "0.56"
                }
              },
              {
                "date": "2Q2020",
                "actual": {
                  "raw": 0.64,
                  "fmt": "0.64"
                },
                "estimate": {
                  "raw": 0.51,
                  "fmt": "0.51"
                }
              }
            ],
            "currentQuarterEstimate": {
              "raw": 0.98,
              "fmt": "0.98"
            },
            "currentQuarterEstimateDate": "1Q",
            "currentQuarterEstimateYear": 2021,
            "earningsDate": [
              {
                "raw": 1619568000,
                "fmt": "2021-04-28"
              },
              {
                "raw": 1620000000,
                "fmt": "2021-05-03"
              }
            ]
          },
          "financialsChart": {
            "yearly": [
              {
                "date": 2017,
                "revenue": {
                  "raw": 229234000000,
                  "fmt": "229.23B",
                  "longFmt": "229,234,000,000"
                },
                "earnings": {
                  "raw": 48351000000,
                  "fmt": "48.35B",
                  "longFmt": "48,351,000,000"
                }
              },
              {
                "date": 2018,
                "revenue": {
                  "raw": 265595000000,
                  "fmt": "265.6B",
                  "longFmt": "265,595,000,000"
                },
                "earnings": {
                  "raw": 59531000000,
                  "fmt": "59.53B",
                  "longFmt": "59,531,000,000"
                }
              },
              {
                "date": 2019,
                "revenue": {
                  "raw": 260174000000,
                  "fmt": "260.17B",
                  "longFmt": "260,174,000,000"
                },
                "earnings": {
                  "raw": 55256000000,
                  "fmt": "55.26B",
                  "longFmt": "55,256,000,000"
                }
              },
              {
                "date": 2020,
                "revenue": {
                  "raw": 274515000000,
                  "fmt": "274.51B",
                  "longFmt": "274,515,000,000"
                },
                "earnings": {
                  "raw": 57411000000,
                  "fmt": "57.41B",
                  "longFmt": "57,411,000,000"
                }
              }
            ],
            "quarterly": [
              {
                "date": "1Q2020",
                "revenue": {
                  "raw": 58313000000,
                  "fmt": "58.31B",
                  "longFmt": "58,313,000,000"
                },
                "earnings": {
                  "raw": 11249000000,
                  "fmt": "11.25B",
                  "longFmt": "11,249,000,000"
                }
              },
              {
                "date": "2Q2020",
                "revenue": {
                  "raw": 59685000000,
                  "fmt": "59.69B",
                  "longFmt": "59,685,000,000"
                },
                "earnings": {
                  "raw": 11253000000,
                  "fmt": "11.25B",
                  "longFmt": "11,253,000,000"
                }
              },
              {
                "date": "3Q2020",
                "revenue": {
                  "raw": 64698000000,
                  "fmt": "64.7B",
                  "longFmt": "64,698,000,000"
                },
                "earnings": {
                  "raw": 12673000000,
                  "fmt": "12.67B",
                  "longFmt": "12,673,000,000"
                }
              },
              {
                "date": "4Q2020",
                "revenue": {
                  "raw": 111439000000,
                  "fmt": "111.44B",
                  "longFmt": "111,439,000,000"
                },
                "earnings": {
                  "raw": 28755000000,
                  "fmt": "28.75B",
                  "longFmt": "28,755,000,000"
                }
              }
            ]
          },
          "financialCurrency": "USD"
        }
      }
    ],
    "error": null
  }
}

I am trying to run like below

$json = file_get_contents('my.json');
$data = json_decode($json, true);

echo $data['quoteSummary']['result']['summaryDetail']['priceHint']['raw'];

but its giving me warning like

 Notice: Undefined index: summaryDetail in C:\xampp\htdocs\index.php on line 6

Basically I am looking for get below values from my json file

From summaryDetail, I want 
maxAge
priceHint->raw

From defaultKeyStatistics, I want
lastDividendDate->raw

and From Earning, I want
earningsChart->quarterly->actual->raw

Let me know if anyone can help me for same. I am trying from last hour but not able to get idea what I should do .

Thanks!

Mira
  • 87
  • 5

1 Answers1

0

Your result node is an array of objects, so it be probably:

echo $data['quoteSummary']['result'][0]['summaryDetail']['priceHint']['raw'];

biesior
  • 55,576
  • 10
  • 125
  • 182
  • Thanks, Its working great, can You please help me for my other values? Thanks! – Mira Feb 24 '21 at 00:17
  • 1
    No, I gave you a clue, you will find solution yourself without problems. BTW is that array required as it contains only one element??? – biesior Feb 24 '21 at 00:20
  • Its third party json and so I will not able to modify it. Thanks! – Mira Feb 24 '21 at 00:22
  • 1
    @Mira curly bracket `{` starts object, square bracket `[` starts an array. Objects nodes are targeted by their keys like `quoteSummary `, `result `, etc. Array elements are counted one by one starting from 0 – biesior Feb 24 '21 at 00:26
  • You don't need to modify it. Just most inetersting part for you starts here: `$data['quoteSummary']['result'][0]` - later you can add more paths like `['summaryDetail']['previousClose']['fmt']` etc – biesior Feb 24 '21 at 00:30
  • I'd suggest using some IDE or inspector which is able to highlight coresponding pairs its, that helps a lot. – biesior Feb 24 '21 at 00:32