如何使用空格访问JSON对象?

时间:2018-01-13 07:46:44

标签: php arrays json

我试图访问以下输出JSON,以及我是如何做到的:

  date(DATE_SUB(deadline, INTERVAL 1 MONTH))

这很好用。问题是我想循环使用几个月的数据,所以我不能轻易地将它循环到中间对象[' 2018-01-12'],我想要一个我的for循环更简单的方法。以下不起作用(比如访问该时间序列数组中的第5个元素),但我想要类似的东西。

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo

$string = file_get_contents("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo");

$json_b = json_decode($string, true);

echo $json_b['Time Series (Daily)']['2018-01-12']['4. close'];

这可能吗?

3 个答案:

答案 0 :(得分:1)

或许以下内容可能会提供一些指导,尤其是效用函数(getitem),以便从数据中获取特定项目

<?php
    $url='https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo';
    $json=json_decode( file_get_contents( $url ), true );

    function pre($data=false,$header=false){
        if( $data ){
            $title = $header ? sprintf('<h1>%s</h1>',$header) : '';
            printf('%s<pre>%s</pre>',$title,print_r($data,1));
        }
    }

    function getitem( $obj, $key, $item ){
        return isset( $obj[ $key ] ) && isset( $obj[ $key ][ $item ] ) ? $obj[ $key ][ $item ] : false;
    }
?>
<!doctype html>
<html>
    <head>
        <meta charset='utf-8' />
        <title>JSON-Array-Data Loopity loop</title>
    </head>
    <body>
        <?php
            $meta=$json['Meta Data'];
            $times=$json['Time Series (Daily)'];

            /* all close prices for last n days */
            $days=30;

            $dnow=new DateTime();

            foreach( $times as $key => $obj ){
                $dkey=new DateTime( $key );
                if( $dnow->diff( $dkey )->days < $days ){
                    printf('Close price: %s<br />', getitem( $times, $key, '4. close') );
                }
            }

            /* Get a specific record */
            echo getitem( $times, '2018-01-11', '5. volume' );

            /* show all of type '4. close' */
            foreach( $times as $key => $obj ){
                printf( '%s<br />', getitem( $times, $key, '4. close' ) );
            }

            /* show all records */
            foreach( $times as $key => $obj ){
                pre( $obj, $key );
            }
        ?>
    </body>
</html>

答案 1 :(得分:1)

如果你想要数组的第5个元素

$key = 4;
$target = array_slice($json_b['Time Series (Daily)'], $key,1);
echo key($target) . ' => ' . current($target)['4. close'];

demo

答案 2 :(得分:0)

您可以使用foreach循环在PHP中迭代对象。

<?php
$string = '{
  "Meta Data": {
    "1. Information": "Daily Prices (open, high, low, close) and Volumes",
    "2. Symbol": "MSFT",
    "3. Last Refreshed": "2018-01-12",
    "4. Output Size": "Compact",
    "5. Time Zone": "US/Eastern"
  },
  "Time Series (Daily)": {
    "2018-01-12": {
      "1. open": "88.6700",
      "2. high": "89.7800",
      "3. low": "88.4500",
      "4. close": "89.6000",
      "5. volume": "24260921"
    },
    "2018-01-11": {
      "1. open": "88.1300",
      "2. high": "88.1300",
      "3. low": "87.2400",
      "4. close": "88.0800",
      "5. volume": "16529868"
    }}}';

$json_b = json_decode($string, true);

foreach( $json_b as $key => $value ){
    echo $key."\n";
}