2016-11-15 8 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()の2番目のパラメータを使用する必要があります。その後、アレイと、印刷時間を循環するforeach()を使用することができます。

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

私はこのエラーを取得 - 'PHP致命的なエラー:/var/www/html/fileName.php上での配列としてのタイプはstdClassのオブジェクトを使用することはできませんline 45' – SMG

+0

@SMG: '' echo 'の直前に '$ json = json_decode($ result、true);という行がありますか? –

+0

はい、私は走ったコードでコメントを解除しました! – SMG