2016-11-15 175 views
1

My code is提取数据

$url = "URL"; 
$curl = curl_init(); 
curl_setopt_array($curl, array(
    CURLOPT_URL => $url, 
    CURLOPT_RETURNTRANSFER => 1 
)); 
$result = curl_exec($curl); 
var_dump($result); 
$json = json_decode($result); 
print_r($json); 

curl_close($curl); 

The response from var_dump($result) is -

string(1866) "{"status":true,"result":[{"time":"2016-11-15T19:20:27.000Z"},{"time":"2016-11-15T19:18:15.000Z"},{"time":"2016-11-15T19:15:03.000Z"}, 

The response I get from print_r($json) is -

stdClass Object 
(
    [status] => 1 
    [result] => Array 
    (
     [0] => stdClass Object 
      (
       [time] => 2016-11-15T19:20:27.000Z 
      ) 

     [1] => stdClass Object 
      (
       [time] => 2016-11-15T19:18:15.000Z 
      ) 

     [2] => stdClass Object 
      (
       [time] => 2016-11-15T19:15:03.000Z 
      ) 

我需要的时间的值成一些变量。

Something I have done in Javscript is -

var response = JSON.parse(xmlHttpSerie.responseText); 
response.forEach(function(items) 
{ 
    currentTime = items.time; 
} 

谁能告诉我如何从一个变量的响应得到了时间的价值?

回答

1

echo $json['result'][0]['time'];

EDIT after major changes to the original question:

您需要使用的json_decode()第二个参数转换对象关联数组。然后你可以使用foreach()循环遍历数组并打印时间:

$json = json_decode($result, TRUE); 
foreach ($json['result'] as $index => $v) { 
    echo $v['time'].'<br>'; 
}  
+0

我得到这个错误 - 'PHP致命错误:在/var/www/html/fileName.php上不能使用类型为stdClass的对象为数组第45行' – SMG

+0

@SMG:你确定你有'$ json = json_decode($ result,true)'行吗?'就在'echo'之前? –

+0

是的,在我跑过的代码中没有评论它! – SMG