如何在PHP中解析JSON数组并在HTML表中显示?
我需要在PHP中解析JSON数组响应,并在HTML表中回显这些值。我对PHP-JSON非常陌生,我想不出一种方法来做到这一点如何在PHP中解析JSON数组并在HTML表中显示?,php,arrays,json,curl,php-curl,Php,Arrays,Json,Curl,Php Curl,我需要在PHP中解析JSON数组响应,并在HTML表中回显这些值。我对PHP-JSON非常陌生,我想不出一种方法来做到这一点 $curl_response = curl_exec($curl); echo $curl_response . PHP_EOL; JSON数组响应: { "employees": [ { "emp_id":101, "name":&quo
$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
}
]
},
]
}
$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>";
}
}
(empty table)
<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>
PHP脚本:
{
"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
}
]
},
]
}
$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>";
}
}
(empty table)
<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>
预期输出:
{
"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
}
]
},
]
}
$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>";
}
}
(empty table)
<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>
102
emp102@example.com
100
B
103
emp103@example.com
15
B
有人,请帮我找出剧本的毛病。有没有更好的方法可以更快地执行脚本并减少流量负载?您正在使用的
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']作为$data)
{
如果($json['employees'][0]['Attention']['days_present']]==0){
//排除当前天数为0的员工
}否则{
回声“;
echo“$json['employees']['emp_id']”;
echo“$json['employees']['email']”;
echo“$json['employees']['days_present']”;
echo“$json['employees'][0]['attendace']['grade']”;
回声“;
}
}
到
foreach($json['employees']作为$data)
{
如果($data['ATSINGENT'][0]['days\U present']==0){
//排除当前天数为0的员工
}否则{
回声“;
echo“$data['emp_id']”;
回显“$data['email']”;
echo“$data['attention'][0]['days_present']”;
echo“$data['attention'][0]['grade']”;
回声“;
}
}
非常简单的问题;我重写了您提供的整个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"
}
}
]
}
1st:您的[“出勤”][“成绩”]
值周围没有引号。任何包含非字母数字字符或字母字符的内容都需要引用。只有数值可以不加引号(如果我错了,请纠正我,但我很确定这是怎么回事)
2nd:在[“考勤”]
值的周围有方括号,这将它放在一个未命名的维度中,这实际上是不必要的,只是使得检索它有点困难
3rd:底部最后一个逗号如下:},
是您的json\u解码没有得到任何响应的另一个原因。删除最后一个毫无意义的逗号,并至少执行第一步,它应该会起作用
下面是您的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
]
}
{
“雇员”:[
{
“emp_id”:101,
“姓名”:“基督约瑟夫”,
“电子邮件”:emp101@example.com",
“出席情况”:[#哈哈,我甚至没有走那么远,但这确实是另一个问题:P我试图测试他的一些代码,但我甚至无法解码他提供的JSON,似乎是无效的。按照你的答案。但最后两列显示为空。我已经修复了最后两列的代码,尽管@Crimin4L说,我怀疑它不会起任何作用是的,因为JSON有问题。我调整并批准了@Crimin4L的编辑。对数组变量的连接而不是插值考虑得很好。我将数组引用保留在$data['Attention']
由于原始海报的“JSON”以数组形式显示“出席”。哈哈,谢谢@WillWalsh,我想知道你为什么要把它们放在[0]
“在那里,但后来它点击了,我意识到了原因;我在回答中提到的括号只是一个未命名的维度lolol1st-有点正确。PHP将处理带有索引的关联数组,但周围没有引号,但它会抛出警告。最好的做法是包含引号。