2015-06-19 64 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

那真真棒! –