【问题标题】:How to show data to table html from json nested loop array in php如何从php中的json嵌套循环数组向表html显示数据
【发布时间】:2021-11-08 04:08:09
【问题描述】:

首先,对不起,我是 PHP 新手,我已经搜索了解我想要做什么,但找不到任何解决方案。对于专业开发人员来说,我的问题可能看起来很简单。如果它伤害了任何专业程序员的自尊心,请不要关闭,因为任何善良的程序员都可以看到并帮助我。谢谢。

我有来自 curl http 请求的 json 数据,我将其更改为数组:

array(6) {
  ["page"]=>
  int(2)
  ["per_page"]=>
  int(6)
  ["total"]=>
  int(12)
  ["total_pages"]=>
  int(2)
  ["data"]=>
  array(6) {
    [0]=>
    array(5) {
      ["id"]=>
      int(7)
      ["email"]=>
      string(24) "michael.lawson@reqres.in"
      ["first_name"]=>
      string(7) "Michael"
      ["last_name"]=>
      string(6) "Lawson"
      ["avatar"]=>
      string(39) "https://reqres.in/img/faces/7-image.jpg"
    }
    [1]=>
    array(5) {
      ["id"]=>
      int(8)
      ["email"]=>
      string(26) "lindsay.ferguson@reqres.in"
      ["first_name"]=>
      string(7) "Lindsay"
      ["last_name"]=>
      string(8) "Ferguson"
      ["avatar"]=>
      string(39) "https://reqres.in/img/faces/8-image.jpg"
    }
    [2]=>
    array(5) {
      ["id"]=>
      int(9)
      ["email"]=>
      string(22) "tobias.funke@reqres.in"
      ["first_name"]=>
      string(6) "Tobias"
      ["last_name"]=>
      string(5) "Funke"
      ["avatar"]=>
      string(39) "https://reqres.in/img/faces/9-image.jpg"
    }
    [3]=>
    array(5) {
      ["id"]=>
      int(10)
      ["email"]=>
      string(22) "byron.fields@reqres.in"
      ["first_name"]=>
      string(5) "Byron"
      ["last_name"]=>
      string(6) "Fields"
      ["avatar"]=>
      string(40) "https://reqres.in/img/faces/10-image.jpg"
    }
    [4]=>
    array(5) {
      ["id"]=>
      int(11)
      ["email"]=>
      string(24) "george.edwards@reqres.in"
      ["first_name"]=>
      string(6) "George"
      ["last_name"]=>
      string(7) "Edwards"
      ["avatar"]=>
      string(40) "https://reqres.in/img/faces/11-image.jpg"
    }
    [5]=>
    array(5) {
      ["id"]=>
      int(12)
      ["email"]=>
      string(23) "rachel.howell@reqres.in"
      ["first_name"]=>
      string(6) "Rachel"
      ["last_name"]=>
      string(6) "Howell"
      ["avatar"]=>
      string(40) "https://reqres.in/img/faces/12-image.jpg"
    }
  }
  ["support"]=>
  array(2) {
    ["url"]=>
    string(34) "https://reqres.in/#support-heading"
    ["text"]=>
    string(72) "To keep ReqRes free, contributions towards server costs are appreciated!"
  }
}

我想将此数据添加到html中的表中,当我尝试存储数据时,我无法获得存储数据的真实方法。这是我的html:

<table>
        <thead>
            <tr>
                <th>No</th>
                <th>Nama Depan</th>
                <th>Nama Belakang</th>
                <th>ID</th>
                <th>Foto</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($profile as $pro) { ?>
                <?php $no = 1; ?>
            <tr>
                <td><?= $no++; ?></td>
                <td><?= $profile['data'][0]['first_name'] ?></td>
                <td><?= $profile['data'][0]['last_name'] ?></td>
                <td><?= $profile['data'][0]['id'] ?></td>
                <td><?= $profile['data'][0]['avatar'] ?></td>
            </tr>
            <?php } ?>
        </tbody>
    </table>

如何正确存储嵌套数组中的数据?

【问题讨论】:

  • this 之类的东西可以让你继续前进......

标签: php html arrays


【解决方案1】:

如果您的数组保存在$profile 变量中,那么您要显示的数组是$profile['data']。此外,在此特定数据结构的循环中使用静态地址 [0] 也是错误的。 那里没有额外的阵列。循环中的$pro 变量是此结构中的最后一个数组,这是您读取特定字段的地方。

第二件事是,我认为您错误地使用了$no 变量。可能表中的每一行在该字段中都有值 2,对吧?你应该在循环之前传递这个变量的初始化。

在我看来,在将 HTML 与 PHP 混合使用时,最好使用由endforeach 封闭的foreach (something):,而不是花括号,这样更具可读性。

试试这段代码(我在其中添加了一些关于 $no 变量的 cmets)

<table>
    <thead>
        <tr>
            <th>No</th>
            <th>Nama Depan</th>
            <th>Nama Belakang</th>
            <th>ID</th>
            <th>Foto</th>
        </tr>
    </thead>
    <tbody>
        <?php $no = 0; /* initialization as 0 before loop */ ?>
        <?php foreach ($profile['data'] as $pro): ?>                
        <?php $no++; /* manipulating the value where it is displayed is not a good idea */ ?>
        <tr>
            <td><?= $no ?></td>
            <td><?= $pro['first_name'] ?></td>
            <td><?= $pro['last_name'] ?></td>
            <td><?= $pro['id'] ?></td>
            <td><?= $pro['avatar'] ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2021-11-02
    相关资源
    最近更新 更多