0

类似于这篇文章:iteration through a stdClass object in PHP

但我只需要获取此 stdClass 对象的 Date [2020-03-20],[2020-02-28] 变量:

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
        ) ...

所以,到目前为止我可以得到 $open, $high, ... 变量,但不能得到日期变量。

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

谢谢

4

1 回答 1

0

这很简单,我不敢相信我以前没有考虑过这个,我只是将数组从:foreach($dataForAllDays as $obj){

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

$date ( $key ) 是我想要的日期变量。

于 2020-03-22T09:57:59.183 回答