【问题标题】:Create table from Array [duplicate]从数组创建表[重复]
【发布时间】:2019-02-15 20:26:04
【问题描述】:

我是 PHP 新手。我发现了一个简单的 foreach 的用法,但我想要更多的进步。 我到现在为止所做的。 我正在使用“get”表单方法从 API 集成中获取一些数据。 我将这些数据转换为数组。每次我有更多的数组。 现在假设我有 4967 个数组。之后也许我会拥有更少或更多。

所以我得到了这个

1 --> Login
2 --> Position ID
3 --> ....
4 --> ...
..
..
13 --> Margin
14 --> Login number (let's say 2005)
15 --> Position Id number (let's say 100)

所以我特别想要像下面这样的东西。每次创建一个 TR 然后 13 TH 并循环它直到结束

<table>
<tbody>
<tr>
<th>Login</th>
<th>Position ID</th>
..
...
<th>Margin</th>
</tr>
<tr>
<th>12435</th>
<th>132321</th>
..
...
<th>2323</th>
</tr>
<tr>
<th>342243</th>
<th>345345</th>
..
...
<th>24324</th>
</tr>
</tbody>
</table>

如果你有好意,请给我一些代码指导。我正在阅读 3 天并尝试,但不幸的是我做不到。

我在 php 代码中创建数组的最后一件事就是这样

$array = explode('\r\n', $encodejson);

输出是这样的

array (
  0 => '"login',
  1 => 'positionId',
  2 => 'openTimestamp',
  3 => 'entryPrice',
  4 => 'direction',
  5 => 'volume',
  6 => 'symbol',
  7 => 'commission',
  8 => 'swap',
  9 => 'bookType',
  10 => 'stake',
  11 => 'spreadBetting',
  12 => 'usedMargin',
  13 => '3004701',
  14 => '394254',
  15 => '2018-07-19T23:23:53.733',
  16 => '1.2495',
  17 => 'BUY',
  18 => '300000.00',
  19 => 'GBPUSD',
  20 => '1.36',
  21 => '0.00',
  22 => 'BOOK_B',
  23 => '0.00',
  24 => 'false',
  25 => '5325.30',

0 - 12 是 tr th 13 -25 是另一个 tr th

这种情况一直持续到最后(现在假设我有 2483 个具有这种结构的数组)

最后更新!!! 我用这种方式完成了它并工作了

$chunks = array_chunk($array, 13);

    echo '<table id = "customers">';
    foreach ($chunks as $chunk) {
        echo '<tr>';
        foreach ($chunk as $val) {
            printf('<td>%s</td>', $val);
        }
        echo '</tr>';
    }
    echo '</table>';

感谢大家的帮助

【问题讨论】:

  • 请告诉我们var_export($array);的输出
  • 感谢您的回答。我已经编辑了我的原始帖子。请看

标签: php for-loop foreach html-table


【解决方案1】:

也许您可以尝试从大数组中生成块。

$chunks = array_chunk($array, 13);
foreach($chunks as $chunk) {
   //form the table here
   echo "<tr>";
   foreach($chuck as $value){
      echo "<th>".$value."</th>";
   }
   echo "</tr>";
}

修改平面数组 intu 块后,您将分离出模拟 1 行的子数组。

【讨论】:

  • 我现在可以这样打印了 [ "\"login", "positionId", "openTimestamp", "entryPrice", "direction", "volume", "symbol", "commission", “swap”、“bookType”、“stake”、“spreadBetting”、“usedMargin”]、[“3004701”、“394254”、“2018-07-19T23:23:53.733”、“1.2495”、“BUY”、 “300000.00”、“GBPUSD”、“1.36”、“0.00”、“BOOK_B”、“0.00”、“假”、“5325.30”]、
  • 非常感谢我的朋友 :) 太简单了 :)
猜你喜欢
  • 1970-01-01
  • 2015-08-21
  • 2012-09-22
  • 2015-11-19
  • 1970-01-01
  • 2021-10-26
  • 2021-03-09
  • 1970-01-01
  • 2016-08-24
相关资源
最近更新 更多