I need to parse a JSON Array Response in PHP and echo the values in an HTML Table. I'm very new to PHP-JSON, I'm unable to figure out a way to do so.
$curl_response = curl_exec($curl);
echo $curl_response . PHP_EOL;
JSON Array Response :
{
"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 Script :
$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>";
}
}
Getting Output:
(empty table)
Expecting 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>
Someone, please help me to figure out what's wrong with the script. Is there any better method available to execute the script faster and with less traffic load?