Php 在Laravel 5.1中以表格格式显示数组值
我有这样一个数组:Php 在Laravel 5.1中以表格格式显示数组值,php,arrays,laravel,laravel-5.1,html-table,Php,Arrays,Laravel,Laravel 5.1,Html Table,我有这样一个数组: 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" "se
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
身份证件
细节
净价
发票人
佣金
主持人委员会
首席主持人委员会
附属委员会
@foreach($products as$product)
{{$product['product_id']}
产品代码:{{$Product['Product_Code']}}
@如果($product['option_code']!='')
选项代码:{{$product['Option_Code']}
@恩迪夫
{{$product['净价']}
@如果($product['accountType']='seller')
{{number_格式($product['sellerCommission'],2)}
@否则
--
@恩迪夫
@如果($product['accountType']=='慢化剂')
{{number_格式($product['mediatorcommission'],2)}
@否则
--
@恩迪夫
@如果($product['ACCOUNTYPE']=='PRINCIAL\U CONDIMENTOR')
{{number_格式($product['principalModeratorCommission'],2)}
@否则
--
@恩迪夫
@如果($product['accountType']=='affiliate')
{{number_格式($product['affiliateCommission'],2)}
@否则
--
@恩迪夫
@endforeach
上面的代码可以工作,但它向表中添加了一个新行,这是我不需要的。我不想添加新行,而是只想在该行中显示所需的数据
也就是说,对于产品id=24
,(选项代码除外),有2个账户类型
,即附属机构
和负责人
。我想用该数据填充一行,而不是两个不同的行
我如何实现它
我知道这一定很容易,但我没能解决它,因为我还处于学习阶段
非常感谢您的帮助。谢谢
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属性中是否存在该类型。首先,我建议您为性能原因对该查询进行分页。我已经使用if-else语句中的插件完成了这项工作,我想问题在于if-else语句中的
$product['accountType']
。请重新访问。是什么问题,你能给我指点正确的方法吗?你的解决方案有效,但有一个问题。。在数组中,某些产品具有相同的产品id
,但具有不同的选项\u code
(默认值为空),我希望它们也显示出来。。如何操作?在修改已存在的分组产品时,请检查选项\代码是否为空,如果不是空,则将其设置为分组产品<代码>如果($product['option_code']!=''{$GROUBLED[$product['product_id']]['option_code']=$product['option_code'];}应该可以正常工作,只要每个产品只有一个选项代码。一个产品可以有多个选项代码
。。例如,可能有一块布-产品1-具有不同尺寸[opt-1=>“opt-1-1”,opt-1=>“opt-1-2”等],具有多个选项代码,此处(opt-1-1,opt-1-2)。。我该怎么做呢?和accountType的方法一样-将它转换为数组并不断添加新的。好的。。那我怎么检查呢??