2017-10-17 59 views
0

在下面的JSON retured数组中,我希望访问最近两个日期的收盘价($ json ['Time Series(Daily)'] [some date] ['4。close'])。由于数据是股票价格,日期每天都在变化。周末和公众假期也没有数据。 我可以访问当天的数据,但是如何访问前一天的数据? 我认为通过索引访问元素将工作,但我不知道如何通过在PHP索引访问JSON元素。 有没有其他办法可以做我想做的事情? 我的JSON返回的数组看起来如下: $ JSON = 阵列 ( [元数据] =>数组 ( [1.信息] =>每日价格(开放,高,低,接近)和卷 [2.符号] => AAPL [3.最后一次刷新] => 2017年10月16日 [4输出尺寸] =>紧凑 [5.时区] => US /东部 )如何通过PHP中的索引访问json数据?

[Time Series (Daily)] => Array 
    (
     [2017-10-16] => Array 
      (
       [1. open] => 157.9000 
       [2. high] => 160.0000 
       [3. low] => 157.6500 
       [4. close] => 159.8800 
       [5. volume] => 23894630 
      ) 

     [2017-10-13] => Array 
      (
       [1. open] => 156.7300 
       [2. high] => 157.2800 
       [3. low] => 156.4100 
       [4. close] => 156.9900 
       [5. volume] => 16287608 
      ) 

     [2017-10-12] => Array 
      (
       [1. open] => 156.3500 
       [2. high] => 157.3700 
       [3. low] => 155.7300 
       [4. close] => 156.0000 
       [5. volume] => 16045720 
      ) 

     [2017-10-11] => Array 
      (
       [1. open] => 155.9700 
       [2. high] => 156.9800 
       [3. low] => 155.7500 
       [4. close] => 156.5500 
       [5. volume] => 16607693 
      ) 

     [2017-10-10] => Array 
      (
       [1. open] => 156.0600 
       [2. high] => 158.0000 
       [3. low] => 155.1000 
       [4. close] => 155.9000 
       [5. volume] => 15456331 
      ) 

............等

+0

如果关键字是列表类型,但是以日期为关键字,则可以通过索引访问json数组,或者您需要直接了解日期或循环访问数组在foreach循环中 – catzilla

+0

@catzilla It如果您可以详细说明并提供示例代码 – user45437

+0

我已经添加了答案,您可以检查 – catzilla

回答

-1

首先你可以在PHP像然后解码JSON,

$array = json_decode($json_array,true); 

后,您可以通过键来访问它,每次用或直接的价值一样,

$array->2017-10-16->1. open 
+0

我已经使用json_decode()。结果是我上面发布的json数组。日期会每天更改,所以我不想使用以下命令访问数据:$ array-> 2017-10-16-> 1。打开。相反,我想通过索引来访问日期。 – user45437

+0

Ohkey然后你应该尝试你的数组array_slice像array_slice($ json_array,0,3)它可以给你第一个两个对象,然后你可以foreach,foreach($ new_arr作为$ values){$ close_values [] = $ value-> 4。关; } print _r($ close_values); – Rits

0

你可以通过数组排序日期键见(http://php.net/manual/en/function.uksort.php

,然后检索数组的最后一个元件

例如:(i没有测试此)

<?php 
function cmp($a, $b) 
{ 
    if ($a[1] == $b[1]) return 0; 
    return (strtotime($a[1]) < strtotime($b[1])) ? 1 : -1; 
} 

// order the array by date 
uksort($json, "cmp"); 

end($array);   // move the internal pointer to the end of the array 
$date = key($array); // fetches the key of the element pointed to by the internal pointer 
$values = array_pop($array); // retrieve the last element of the array 

//do it agian for the day before 
end($array);   // move the internal pointer to the end of the array 
$prevDate = key($array); // fetches the key of the element pointed to by the internal pointer 
$prevValues = array_pop($array); // retrieve the last element of the array 

var_dump($date,$prevDate); 
?> 
+0

我想在比较日期之前,我需要知道日期。我如何访问日期? – user45437

+0

我已经更新了我的代码示例,这是您的意思?如果你想要数组中的所有日期,你可以使用像array_keys()这样的函数。参见(http://php.net/manual/en/function.array-keys.php) – Tim

0

假设$json阵列已下令日期,

上有访问$json阵列上,date为关键的几个方法。

strftimestrptime会大大帮助你。

我会建议创建一个日期移动功能,接受date (string)并根据您想要的移动返回修改的date (string)

例如:

function adjustDate($date, $move) { 
    $dateObject = // uses strptime to create date object from the $date 
    $dateObject += $move; // $move is an integer - to revert back, + to move forward 
    return //strftime -> this should format the date object to the string 
} 

现在访问$json阵列应该是一个简单的问题,现在。

+0

什么参数将决定$ move值?此举必须是-1即。访问前一个日期。这项工作只在周二至周五之间进行。在这里,我无法获取有关日期的信息。那么,什么参数将决定我们的行动? – user45437