在Laravel 5.1中以表格格式显示数组值


Display array values in table format in Laravel 5.1

我有一个这样的数组:

array:55 [▼
  0 => array:13 [▼
    "product_id" => "1"
    "name" => "Glowing Antique Multi-Color Necklace And Hangings"
    "product_code" => "DIV-COL-0001-0001"
    "option_code" => ""
    "net_price" => "693.00"
    "sellerCommission" => "450.00"
    "moderatorCommission" => "14.00"
    "principalModeratorCommission" => "17.00"
    "affiliateCommission" => "28.00"
    "accountType" => "affiliate"
  ]
  25 => array:13 [▼
    "product_id" => "24"
    "name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
    "product_code" => "PRA-KEN-0002-0006"
    "option_code" => ""
    "net_price" => "184.62"
    "sellerCommission" => "120.00"
    "moderatorCommission" => "4.00"
    "principalModeratorCommission" => "5.00"
    "affiliateCommission" => "7.00"
    "accountType" => "affiliate"
  ]
  26 => array:13 [▼
    "product_id" => "25"
    "name" => "Side Hanging Purse (2 Nos.)"
    "product_code" => "PRA-KEN-0002-0007"
    "option_code" => ""
    "net_price" => "184.62"
    "sellerCommission" => "120.00"
    "moderatorCommission" => "4.00"
    "principalModeratorCommission" => "5.00"
    "affiliateCommission" => "7.00"
    "accountType" => "affiliate"
 ]
  44 => array:13 [▼
    "product_id" => "24"
    "name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
    "product_code" => "PRA-KEN-0002-0006"
    "option_code" => ""
    "net_price" => "184.62"
    "sellerCommission" => "120.00"
    "moderatorCommission" => "4.00"
    "principalModeratorCommission" => "5.00"
    "affiliateCommission" => "7.00"
    "accountType" => "principal_moderator"
  ]
  // And the list continues ...
]

到目前为止,我尝试过的代码在一定程度上是正确的。

<tr>
    <th style="vertical-align: top;">Id</th>
    <th style="vertical-align: top;">Details</th>
    <th style="vertical-align: top;">Net Price</th>
    <th style="vertical-align: top;">Invoicer<br /> Commission</th>
    <th style="vertical-align: top;">Moderator Commission</th>
    <th style="vertical-align: top;">Principal Moderator Commission</th>
    <th style="vertical-align: top;">Affiliate Commission</th>
</tr>
@foreach($products as $product)
    <tr>
        <td>{{ $product['product_id'] }}</td>
        <td>
            <img src="{{ $product['image'] }}" alt="{{ $product['name'] }}" class="pull-left" style="margin-right: 15px">
            <a href="{{ $product['page_url'] }}" class="links-dark">{{ $product['name'] }}</a><br />
            Product Code: {{ $product['product_code'] }}<br />
            @if($product['option_code'] !== '')
                Option Code: {{ $product['option_code'] }}
            @endif
        </td>
        <td class="text-right">{{ $product['net_price'] }}</td>
        @if($product['accountType'] === 'seller')
            <td class="text-right">{{ number_format($product['sellerCommission'], 2) }}</td>
        @else
            <td class="text-right">--</td>
        @endif
        @if($product['accountType'] === 'moderator')
            <td class="text-right">{{ number_format($product['moderatorCommission'], 2) }}</td>
        @else
            <td class="text-right">--</td>
        @endif
        @if($product['accountType'] === 'principal_moderator')
            <td class="text-right">{{ number_format($product['principalModeratorCommission'], 2) }}</td>
        @else
            <td class="text-right">--</td>
        @endif
        @if($product['accountType'] === 'affiliate')
            <td class="text-right">{{ number_format($product['affiliateCommission'], 2) }}</td>
        @else
            <td class="text-right">--</td>
        @endif
    </tr>
@endforeach

上面的代码有效,但它向表中添加了一个新行,这不是我想要的。我不想添加新行,而是只想在该行中显示所需的数据。

这意味着,对于product_id = 24,选项代码除外),有2个accountType,即affiliateprincipal_moderator。我想用该数据填充一行,而不是两个不同的行。

我该如何实现它??

我知道这一定很容易,但我没能解决,因为我还处于学习阶段。

非常感谢您的帮助。谢谢

p.S.:所有值都来自数据库,子数组的数量可以是无限的。

您应该对行进行分组,并使accountType属性成为一个数组。基本上创建新的数组,循环遍历产品,如果它们还没有添加,则将它们添加到新的数组中,如果它们已经添加,则向其中添加accountType

$grouped = array();
foreach($products as $product) {
    if(!array_key_exists($product['product_id'], $grouped)) {
        $grouped[$product['product_id']] = $product;
        $grouped[$product['product_id']]['accountTypes'] = array($product['accountType']);
    }else {
        $grouped[$product['product_id']]['accountTypes'][] = $product['accountType'];
    }
}

然后使用分组数据显示数据,在accountType检查中,检查accountTypes属性中是否存在该类型。