簡體   English   中英

如何解析 PHP 中的 JSON 數組並顯示在 HTML 表中?

[英]How to parse the JSON Array in PHP and display in an HTML Table?

我需要解析 PHP 中的 JSON 數組響應,並在 HTML 表中回顯這些值。 我對 PHP-JSON 很陌生,我無法找到這樣做的方法。

$curl_response = curl_exec($curl);
echo $curl_response . PHP_EOL;

JSON 陣列響應:

{
    "employees": [
        {
            "emp_id":101,
            "name":"Christ Joseph",
            "email":"emp101@example.com",
            "attendance": [
                {
                    "last_day_present":"13-05-2021",
                    "days_present":0,
                    "grade":A
                }
            ]
        },
        {
            "emp_id":102,
            "name":"Paolo Jobh",
            "email":"emp102@example.com",
            "attendance": [
                {
                    "last_day_present":"13-05-2021",
                    "days_present":100,
                    "grade":B
                }
            ]
        },
        {
            "emp_id":103,
            "name":"Subo Paul",
            "email":"emp103@example.com",
            "attendance": [
                {
                    "last_day_present":"13-05-2021",
                    "days_present":15,
                    "grade":B
                }
            ]
        },
    ]
}

PHP 腳本:

$json = json_decode($curl_response, true);

echo "<table border='1' width='50%' align='center'>";
foreach($json['employees'] as $data)
{
   if($json['employees'][0]['attendance']['days_present'] == 0) {
     //exclude the employees whose days_present is 0
   } else { 
   echo "<tr>";
   echo "<td>$json['employees']['emp_id']</td>";
   echo "<td>$json['employees']['email']</td>";
   echo "<td>$json['employees']['days_present']</td>";
   echo "<td>$json['employees'][0]['attendace']['grade']</td>";
   echo "</tr>";
  }
}

獲取 Output:

(empty table)

期待 Output:

<table border="1" width="50%" align="center">
<!-- Display the JSON array response here -->
<tr>
  <td>102</td>
  <td>emp102@example.com</td>
  <td>100</td>
  <td>B</td>
</tr>
<tr>
  <td>103</td>
  <td>emp103@example.com</td>
  <td>15</td>
  <td>B</td>
</tr>

有人,請幫我弄清楚腳本有什么問題。 有沒有更好的方法可以更快地執行腳本並減少流量負載?

您正在使用的foreach/as循環將$json['employees']數組的每個元素放入一個名為$data的變量中。 您需要像這樣調整代碼。

改變

foreach($json['employees'] as $data)
{
   if($json['employees'][0]['attendance']['days_present'] == 0) {
     //exclude the employees whose days_present is 0
   } else { 
   echo "<tr>";
   echo "<td>$json['employees']['emp_id']</td>";
   echo "<td>$json['employees']['email']</td>";
   echo "<td>$json['employees']['days_present']</td>";
   echo "<td>$json['employees'][0]['attendace']['grade']</td>";
   echo "</tr>";
  }
}

foreach($json['employees'] as $data)
{
   if($data['attendance'][0]['days_present'] == 0) {
     //exclude the employees whose days_present is 0
   } else { 
   echo "<tr>";
   echo "<td>" . $data['emp_id'] . "</td>";
   echo "<td>" . $data['email'] . "</td>";
   echo "<td>" . $data['attendance'][0]['days_present'] . "</td>";
   echo "<td>" . $data['attendance'][0]['grade'] . "</td>";
   echo "</tr>";
  }
}

很簡單的問題; 我重寫了您提供的整個 json 數組並將其編碼為 JSON 以查看正確的結構,該結構應如下所示:

{
    "employees": [
        {
            "emp_id": 101,
            "name": "Christ Joseph",
            "email": "emp101@example.com",
            "attendance": {
                "last_day_present": "13-05-2021",
                "days_present": 0,
                "grade": "A"
            }
        },
        {
            "emp_id": 102,
            "name": "Paolo Jobh",
            "email": "emp102@example.com",
            "attendance": {
                "last_day_present": "13-05-2021",
                "days_present": 100,
                "grade": "B"
            }
        },
        {
            "emp_id": 103,
            "name": "Subo Paul",
            "email": "emp103@example.com",
            "attendance": {
                "last_day_present": "13-05-2021",
                "days_present": 15,
                "grade": "B"
            }
        }
    ]
}

第一:您的["attendance"]["grade"]值沒有引用。 任何包含非字母數字字符或字母字符的內容都需要引號。 只有數字值可以不加引號(如果我錯了,請糾正我,但我很確定這是怎么回事)

第二:["attendance"]值周圍有方括號,這將它放在一個未命名的維度中,這確實是不必要的,只會使檢索它有點困難。

第三:底部最后一個逗號如下所示: },是您沒有從json_decode得到任何響應的另一個原因。 刪除最后一個毫無意義的逗號並至少執行第一步,它應該可以工作。

這是您的JSON 數組,其中指出了錯誤:

{
        "employees": [
            {
                "emp_id":101,
                "name":"Christ Joseph",
                "email":"emp101@example.com",
                "attendance": [ # <---------- 2nd Problem 
                    {
                        "last_day_present":"13-05-2021",
                        "days_present":0,
                        "grade":A # <---------- 1st Problem
                    }
                ] # <---------- 2nd Problem
            },
            {
                "emp_id":102,
                "name":"Paolo Jobh",
                "email":"emp102@example.com",
                "attendance": [ # <---------- 2nd Problem
                    {
                        "last_day_present":"13-05-2021",
                        "days_present":100,
                        "grade":B # <---------- 1st Problem
                    }
                ] # <---------- 2nd Problem
            },
            {
                "emp_id":103,
                "name":"Subo Paul",
                "email":"emp103@example.com",
                "attendance": [ # <---------- 2nd Problem
                    {
                        "last_day_present":"13-05-2021",
                        "days_present":15,
                        "grade":B # <---------- 1st Problem
                    }
                ] # <---------- 2nd Problem
            }, # <---------- 3rd Problem
        ]
    }

所以基本上,你的 curl 響應是無效的。 我不確定這是否是您可以控制的,但這看起來像是問題所在。 我把你的整個 JSON 數組放入 sandbox.onlinephpfunctions.com 試圖將它解碼成一個數組並打印出來( 參考文獻),但在我做了上面解釋的幾件事之前它不起作用。

暫無
暫無

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

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