【问题标题】:PHP looping through multilevel Alphavantage API JSONPHP循环通过多级Alphavantage API JSON
【发布时间】:2019-04-07 18:46:17
【问题描述】:

我正在使用来自 Alphavantange.co 的 API 来获取股价值。我需要遍历 API 提供的所有值。

我已从 api 返回 JSON 并使用 json_decode。 我可以得到 1 个值,例如我可以使用下面的代码让 63.3700 回显到屏幕:

<?php
  $string = file_get_contents("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=LLOY.l&outputsize=full&apikey=XXXX");
        $arr = json_decode($string, true);


                            echo $arr['Time Series (Daily)']['2019-04-04']['1. open'].'<br>';
?>

api返回如下(前几条记录的例子)

{
    "Meta Data": {
        "1. Information": "Daily Time Series with Splits and Dividend Events",
        "2. Symbol": "LLOY.l",
        "3. Last Refreshed": "2019-04-05",
         "4. Output Size": "Full size",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2019-04-05": {
            "1. open": "62.4500",
            "2. high": "62.9000",
            "3. low": "62.0800",
            "4. close": "62.2100",
            "5. adjusted close": "62.2100",
            "6. volume": "218007230",
            "7. dividend amount": "0.0000",
            "8. split coefficient": "1.0000"
    },
    "2019-04-04": {
        "1. open": "63.3700",
        "2. high": "63.3800",
        "3. low": "62.3500",
        "4. close": "62.6200",
        "5. adjusted close": "62.6200",
        "6. volume": "193406609",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"
    },
    "2019-04-03": {
        "1. open": "64.1200",
        "2. high": "65.5400",
        "3. low": "63.9300",
        "4. close": "64.8800",
        "5. adjusted close": "62.7400",
        "6. volume": "231702090",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"

我一次可以获取一个值,但最终需要遍历所有值以将它们写入 MySQL 表。当每个级别都有不同的“名称”(不同的日期)时,我如何循环它们?

作为第一个帮助,我将如何输出每个日期的 Open 值,例如 2019-04-05 62.4500 2019-04-04 63.3700 2019-04-03 64.1200

【问题讨论】:

  • 我希望能够输出带有日期的 JSON 中返回的每个“打开”值。为问题添加了更多信息。谢谢
  • 那么foreach( $arr['Time Series (Daily)'] AS $date =&gt; $results) { foreach( $results AS $key =&gt; $value ) { echo '&lt;br&gt;For ' . $key . ' on ' . $date . ' the value is ' . $value; } }
  • @cale_b 这很有帮助。给我足够的信息,让我开始了解如何解决我必须达到目标的问题,并希望继续该项目。你想添加它作为答案,我会 +1。谢谢。

标签: php json multidimensional-array


【解决方案1】:

您可以遍历数组以访问它的所有元素。由于数据的结构,您可能需要这样的嵌套循环:

$arr = json_decode( $string, TRUE );

// check if the array key exists
if ( ! empty( $arr['Time Series (Daily)'] ) {
    // loop over the contents of Time Series
    foreach( $arr['Time Series (Daily)'] AS $date => $results ) { 
        // loop over the results for each date in Time Series
        foreach( $results AS $key => $value ) { 
            echo '<br>For ' . $key . ' on ' . $date . ' the value is ' . $value; 
        } 
    }
}

【讨论】:

    【解决方案2】:

    您必须将 JSON 解码为数组,您可以使用函数 json_decode 将 JSON 转换为数组并在循环中应用逻辑

    $responseToArray = json_decode($response, TRUE);//$response is JSON
    

    现在你可以使用循环了

    foreach($responseToArray as $key => $value){
         /* 
          Your Code here, you can further do the
          loop through $value ...and so on
          */
    }
    

    详情请参阅PHP manual

    【讨论】:

      猜你喜欢
      • 2019-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 2019-12-09
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多