2015-06-19 62 views
-2

多JSON響應我想獲取來自不同服務器的數據庫中的數據,我需要在一個集中的服務器中。所以我寫了一個SELECT查詢在不同的位置,我試圖在一個集中式服務器中讀取。 我得到一個JSON陣列響應。爲HTML格式表

但我想在一個HTML表格式顯示出來,但我無法得到正確的結果。

這是我的中央服務器的PHP代碼:

<?php 
    $arr = array (
        'http://example.com/pristatus/send-curl.php', 
        'http://example2.com/pristatus/send-curl.php' 
       ); 

       for ($i =0; $i<count($arr); $i++) 
       { 

        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $arr[$i]); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
       $output = curl_exec($ch); 

      if($output!="error"){ 
        $data = json_decode($output); 
       } 
       echo "<pre>"; 
       print_r($data); 
       curl_close($ch); 
       } 
    ?> 

輸出我得到:

Array 
(
    [0] => stdClass Object 
     (
      [id] => 9 
      [status] => OK 
      [batch] => 119677 
      [location] => Hyderabad 
      [createdDT] => 2015-06-19 20:40:05 
     ) 

) 
Array 
(
    [0] => stdClass Object 
     (
      [id] => 1 
      [status] => OK 
      [batch] => 56339 
      [location] => Mumbai 
      [createdDT] => 2015-06-19 20:40:05 
     ) 

    [1] => stdClass Object 
     (
      [id] => 2 
      [status] => OK 
      [batch] => 56339 
      [location] => Mumbai 
      [createdDT] => 2015-06-19 20:40:05 
     )  
) 

請建議我我如何可以顯示一個表格式的JSON響應。

+0

使用[的foreach(http://php.net/manual/en/control- structures.foreach.php)。 – Alex

+0

如果JSON使用'json_decode()' –

回答

0

你需要使用foreach循環和比獲得價值一樣

foreach($array as $val) 

$val->property 
0

例子:

echo '<table>'; 
foreach($data as $key) { 
    echo '<tr>'; 
    echo '<td>'.$key->{'id'}.'<td>'; 
    echo '<td>'.$key->{'status'}.'</td>'; 
    echo '<td>'.$key->{'batch'}.'</td>'; 
    echo '<td>'.$key->{'location'}.'</td>'; 
    echo '<td>'.$key->{'createdDT'}.'</td>'; 
    echo '</tr>'; 
} 
echo '</table>'; 
+0

那真真棒! –