2017-03-10 10 views
0

APIの使用複数の配列でJSON応答を取得しています。 HTMLテーブルにレスポンスを表示する必要があります。しかし、私がPHP-JSONを初めて使い慣れたので、私はそうする方法を見つけ出すことができません。PHPで複数のJSON配列からテーブル行を取得する方法は?

APIコール

$url="https://api.domain.com/getdata.php?format=json&token=2455445444&type=ROWS"; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt($ch, CURLOPT_HTTPGET, true); 


    $output = curl_exec($ch); 
    curl_close($ch);  
    $json = json_decode($output, true); 

    $resCode = $json['resCode']; 
    $resText = $json['resText']; 

    //Multiple response 

    $column1 = $json['data']['ROWS'][0]['column1']; 
    $column2 = $json['data']['ROWS'][0]['column2']; 
    $column3 = $json['data']['ROWS'][0]['column3']; 
    $column4 = $json['data']['ROWS'][0]['column4']; 

APIからJSONレスポンスなどの

{ 
    "data":{ 

     "ROWS": [ 
        { 
         "column1":"123", 
         "column2":"456", 
         "column3":"789", 
         "column4":"054" 
        }, 
        { 
         "column1":"775", 
         "column2":"545", 
         "column3":"647", 
         "column4":"745" 
        }, 
        { 
         "column1":"440", 
         "column2":"544", 
         "column3":"745", 
         "column4":"999" 
        } 
       ] 
     }, 

    "resCode":"200", 
    "resText":"SUCCESS" 

} 

I need to display the response like below. How do I do that??

HTML

<table border="1" width="50%" align="center"> 
<tr> 
    <th>Column 1</th> 
    <th>Column 2</th> 
    <th>Column 3</th> 
    <th>Column 4</th> 
</tr> 
<!-- Display the JSON array response here --> 
<!-- Example below --> 
<tr> 
    <td>123</td> 
    <td>456</td> 
    <td>789</td> 
    <td>054</td> 
</tr> 
<tr> 
    <td>775</td> 
    <td>545</td> 
    <td>647</td> 
    <td>745</td> 
</tr> 
<tr> 
    <td>440</td> 
    <td>544</td> 
    <td>745</td> 
    <td>999</td> 
</tr> 
</table> 

答えて

0

エラーを取得するすべての行

echo "<table border="1" width="50%" align="center">"; 
foreach($json['data']['ROWS'] as $data) 
{ 
echo "<tr>"; 
foreach($data as $key=>$value) 
echo "<td>$value</td>"; 
echo "</tr>"; 

} 
+0

を反復処理するのforeachを使用します。警告:foreachのために供給無効な引数() –

+0

あなたは私の本当のリソースで私を助けてくださいことはできますか? –