3

I'm loading a JSON array and decode it to an PHP array

$jsonfile = file_get_contents('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=15min&outputsize=full&apikey=demo'); 
$jsonarray = json_decode($jsonfile); 
var_dump($jsonarray);

So far so good I get an array that looks like that:

object(stdClass)#1 (2) { 
    ["Meta Data"]=> object(stdClass)#2 (5) { 
        ["1. Information"]=> string(49) "Daily Prices (open, high, low, close) and Volumes" 
        ["2. Symbol"]=> string(5) "AAWVX" 
        ["3. Last Refreshed"]=> string(10) "2017-06-30" 
        ["4. Output Size"]=> string(9) "Full size" 
        ["5. Time Zone"]=> string(10) "US/Eastern" 
    } 
    ["Time Series (Daily)"]=> object(stdClass)#3 (105) { 
        ["2017-06-30"]=> object(stdClass)#4 (5) { 
            ["1. open"]=> string(7) "10.5100" 
            ["2. high"]=> string(7) "10.5100" 
            ["3. low"]=> string(7) "10.5100" 
            ["4. close"]=> string(7) "10.5100" 
            ["5. volume"]=> string(1) "0" 
        } 
        ["2017-06-29"]=> object(stdClass)#5 (5) { ["1. open"]=> string(7) "10.4800" ["2. high"]=> string(7) "10.4800" ["3. low"]=> string(7) "10.4800" ["4. close"]=> string(7) "10.4800" ["5. volume"]=> string(1) "0" } 
        ["2017-06-28"]=> object(stdClass)#6 (5) { ["1. open"]=> string(7) "10.5600" ["2. high"]=> string(7) "10.5600" ["3. low"]=> string(7) "10.5600" ["4. close"]=> string(7) "10.5600" ["5. volume"]=> string(1) "0" } ...

But when I try to adress the array like

var_dump($jsonarray['Meta Data']);

It doesn't work.

timiTao
  • 1,417
  • 3
  • 20
  • 34
Christian
  • 848
  • 1
  • 11
  • 23
  • 1
    Either decode with `true` as second arg or use `$jsonarray->{'Meta Data'}->{'1. Information'}` – AbraCadaver Jul 03 '17 at 20:55
  • http://php.net/manual/en/function.json-decode.php See the second argument to the function. Looks like you need it to be true. – ckimbrell Jul 03 '17 at 20:56
  • You need to get in the habit of [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which help you to solve your issues. You'll earn points and others will be encouraged to help you. – Jay Blanchard Jul 03 '17 at 21:03
  • @Jay there's a timer before you can accept an answer. – Christian Jul 03 '17 at 21:05
  • I know, I was just looking at your track record. – Jay Blanchard Jul 03 '17 at 21:07
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Jul 31 '18 at 20:37

1 Answers1

10

This is because json_decode() with no parameters attempts to convert your json string to an stdClass object. If you want to convert it to an array, you need to set the 2nd parameters (the $assoc boolean) to true:

$json = file_get_contents('LINK TO JSON OUTPUT'); 
$array = json_decode($json, true); 
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71