虽然在PHP中遍历stdClass对象,但我无法获得一个变量(Date)

问题描述 投票:0回答:1

类似于此帖子:iterating 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'};

谢谢

php object multidimensional-array stdclass alphavantage
1个回答
0
投票

非常简单,我不敢相信我以前没有考虑过,我只是将Array从:foreach($ dataForAllDays as $ obj){

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

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

© www.soinside.com 2019 - 2024. All rights reserved.