2016-11-15 172 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