代码之家  ›  专栏  ›  技术社区  ›  user805528

一个数组中的多个html表(php)

  •  0
  • user805528  · 技术社区  · 6 年前

    我有一个带有“segment id”和“account id”的元素数组。我想为每个段和每个帐户打印多个HTML表。

    数组:

    ID 1项目10段1账户1

    ID 2项目15段1账户1

    ID 3项目11段2账户1

    ID 4项目16段2账户2

    会打印出3张桌子

    结果:

    表1(ID 1和2)

    表2(ID 3)

    表3(ID 4)

    这可以用嵌套foreach循环完成吗? 对不起,代表不好。我不知道如何在这个编辑器中打印一个可读数组。

    编辑: 一些代码(如果有帮助的话):

    @foreach ($orders as $order)
      <table>
       <thead>
        <tr>
         <th>Product ID</th>
         <th>Segment ID</th>
         <th>Account ID</th>
        </tr>
       </thead>
       <tbody>
        <tr>
         <td>{{ $order->ProductId }}</td>
         <td>{{ $order->SegmentId }}</td>
         <td>{{ $order->AccountId }}</td>
        </tr>
       </tbody>
      </table>
    @endforeach
    

    所以这将给出一个包含所有元素的表。

    我想得到多个html表。foreach segmentid和foreach accountid。

    1 回复  |  直到 6 年前
        1
  •  0
  •   donald123    6 年前

    根据需要拆分阵列

    id 1 | Item 10 | Segment 1 | Account 1
    
    id 2 | Item 15 | Segment 1 | Account 1
    
    id 3 | Item 11 | Segment 2 | Account 1
    
    id 4 | Item 16 | Segment 2 | Account 2
    

    你有这样的阵列

    $array = array('id'=>1,'item'=>10,'segment'=>1,'account'=>1)....
    

    你可以这样做(开箱即用)

    $tables = array();
    foreach($array as $arr) {
       $segment = $arr['segment'];
       $account = $arr['account'];
       $tables[$segment][$account][] = $arr; //UPDATE [] for multiparts
    }
    

    现在您已经在$tables中有了您喜欢的订单,然后您可以继续这样

    foreach($tables as $segment=>$arr) {
       foreach($arr as $account=>$yourdata) {
          echo "<table><tr>...</tr></table>";
       }
    }