【问题标题】:How to parse the JSON Array in PHP and display in an HTML Table?如何在 PHP 中解析 JSON 数组并显示在 HTML 表中?
【发布时间】:2021-08-03 08:13:38
【问题描述】:

我需要在 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>";
  }
}

获取输出:

(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 arrays json curl php-curl


    【解决方案1】:

    您正在使用的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>";
      }
    }
    

    【讨论】:

    • 哈哈,我什至没有走那么远,但这确实是另一个问题:PI 试图测试他的一些代码,但我什至无法解码他提供的 JSON,似乎是无效的.
    • 关注您的回答。但最后两列显示为空。
    • 我已经修复了最后两列的代码,尽管正如@Crimin4L 所说,我怀疑由于 JSON 的问题它无论如何都不会工作。
    • 我调整并批准了 @Crimin4L 的编辑。对数组变量的串联而不是插值的思考很好。由于原始海报的“JSON”将“出席”显示为数组,因此我将数组引用保留在 $data['attendance']
    • 哈哈,谢谢@WillWalsh,我想知道你为什么把它们放在[0] 那里,但它点击了,我意识到为什么;我在回答中提到的那些括号只是一个未命名的维度 lolol
    【解决方案2】:

    很简单的问题;我重写了您提供的整个 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:您的["attendance"]["grade"] 值周围没有引号。任何包含非字母数字字符或字母字符的内容都需要引号。只有数字值可以不加引号(如果我错了,请纠正我,但我很确定这是怎么回事)

    第二个:["attendance"] 值周围有方括号,这将它放在一个未命名的维度中,这实际上是不必要的,只会使检索它有点困难。

    3rd: 底部最后一个逗号如下所示:}, 是您没有收到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 中,试图将它解码成一个数组并打印出来 (ref.),但是直到我做了上面解释的几件事,它才能工作。

    【讨论】:

    • 第一个 - 有些正确。 PHP 将处理带有不带引号的索引的关联数组,但它会抛出警告。最好包含引号。
    猜你喜欢
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2013-08-25
    • 2014-02-05
    • 2018-11-21
    相关资源
    最近更新 更多