0

Similar to this post: iterating through a stdClass object in PHP

But I need to just get the Date [2020-03-20],[2020-02-28] variable of this stdClass Object:

stdClass Object
(
    [2020-03-20] => stdClass Object // <- this is what I need
        (
            [1. open] => 711.2600
            [2. high] => 806.9800
            [3. low] => 350.5100
            [4. close] => 427.5300
            [5. volume] => 298764633
        )

    [2020-02-28] => stdClass Object // <- this is what I need
        (
            [1. open] => 673.6900
            [2. high] => 968.9899
            [3. low] => 611.5200
            [4. close] => 667.9900
            [5. volume] => 473506012
        ) ...

So, far I can get the $open, $high, ... variables, but not the date variable.

$API_KEY = "demo";
$symbol = "TSLA";
$company= "Tesla";
$startDate = "2020-03-01";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,("https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=".$symbol."&apikey=".$API_KEY)); // daily adjusted close
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$result = json_decode($server_output);
    $Array = $result->{'Monthly Time Series'};
    foreach($Array as $obj){
    // THIS IS WHAT I NEED => echo '$date: '.$date.': <BR>'; // THIS DOESN'T WORK, $date is not defined.
$open=$obj->{'1. open'};
    $high=$obj->{'2. high'};
    $low=$obj->{'3. low'};
    $close=$obj->{'4. close'};
    $volume=$obj->{'5. volume'};

Thanks

sebseb
  • 93
  • 1
  • 1
  • 8

1 Answers1

0

It was very simple, I can't believe I didn't think about this before, I just changed the Array from: foreach($dataForAllDays as $obj){

to

foreach($dataForAllDays as $date => $obj){

and the $date ( $key ) is the date variable I wanted.

sebseb
  • 93
  • 1
  • 1
  • 8