簡體   English   中英

PHP - 在 html 表中顯示 JSON 數組

[英]PHP - Show JSON array in html table

我有一個網站的回復

[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}...]

我的代碼是

$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json);
foreach($json_decoded as $result){
...
}

運行此代碼時,出現錯誤Invalid argument supplied for foreach()

這里有什么問題?

如何在html表格中顯示為表格?

這段代碼工作得很好 - 只需在本地機器上測試它

<?php
    $json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
    $json_decoded = json_decode($json);
    foreach($json_decoded as $result){
      print_r($result);
    }
  ?>

要將您的內容輸出為表格,請使用以下命令:

<?php
        $json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
        $json_decoded = json_decode($json);
        echo '<table>';
        foreach($json_decoded as $result){
          echo '<tr>';
            echo '<td>'.$result->name.'</td>';
            echo '<td>'.$result->phone.'</td>';
            echo '<td>'.$result->email.'</td>';
          echo '</tr>';
        }
        echo '</table>';
      ?>

確保您的 JSON 字符串沒有任何語法錯誤……如果是,則 json_decode 將失敗並且 foreach() 循環會引發錯誤。

您在問題中提到的代碼完美無缺,我正在按照表中的要求獲得輸出。 但請確保檢查json_encode()是否正確,以便隱藏foreach錯誤。

您也可以使用我在這里建議的這種方法在表格中打印。

您必須將TRUE應用於json_decode語句,因為 std_object 數組與此相關,因此它會導致打印數組值時出現混淆。

json_decode()

Syntax: json_decode($jsonstring,TRUE);

json_decode — 解碼 JSON 字符串

以適當的 PHP 類型返回以 json 編碼的值。 值 true、false 和 null 分別返回為 TRUE、FALSE 和 NULL。 如果 json 無法解碼或編碼數據比遞歸限制更深,則返回 NULL。

PHP代碼:

<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json,TRUE);
foreach($json_decoded as $result){
    echo $result['name']; // this wil output the values as xxxyyyzzz
}
?>

在 json_decode() 語句中沒有 TRUE 的 print_r() 輸出:

Array ( [0] => stdClass Object ( [name] => xxx [phone] => 123 [email] => a@a.com ) [1] => stdClass Object ( [name] => yyy [phone] => 456 [email] => b@a.com ) [2] => stdClass Object ( [name] => zzz [phone] => 678 [email] => c@a.com ) ) 

在 json_decode() 語句中使用 TRUE 的 print_r() 輸出

Array ( [0] => Array ( [name] => xxx [phone] => 123 [email] => a@a.com ) [1] => Array ( [name] => yyy [phone] => 456 [email] => b@a.com ) [2] => Array ( [name] => zzz [phone] => 678 [email] => c@a.com ) )

因此,上面代碼的輸出如下。

xxxyyyzzz

沒有問題,您可以循環遍歷foreach數據,並且可以使用它進行打印。

打印表中的數據,您可以按照下面提到的代碼進行操作。

<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json,TRUE);
?>
<table border="1">
<thead>
    <tr>
        <th>Name</th>
        <th>Phone</th>
        <th>Email</th>
    </tr>
</thead>    
<tbody>
<?php
    foreach($json_decoded as $result){
        ?>
        <tr>
            
            <td><?php echo $result['name']; ?></td>
            <td><?php echo $result['phone']; ?></td>
            <td><?php echo $result['email']; ?></td>
        </tr>
<?php   
}
?>
</tbody>
</table>

在此處輸入圖片說明

我希望我的解決方案能夠滿足您的需求。

快樂編碼:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM