私が達成しようとしているのは、配列をループすることです。次に、配列内の項目がproduct_id、サイズ値、およびカラー値の3つの点で同じであるかどうかを調べます。 アイテムがリストされている新しい配列を作成したいのですが、唯一必要としないのは重複した値だけです。私はそれらが3つのポイントで同じであれば、重複した値が欲しいと思います。同じ商品IDの同じ商品IDと同じ色の3つの商品と3つの商品の3つの商品が並んでいるように、これは新しい商品で3つの商品を注文したことになります。これは1回だけ表示され、数量は9となります。アレイ。配列の値を同じ配列の他の値と比較する
現在のループ私が探しているものの次の配列
Array
(
[id] => 2
[product_id] => 4
[order_id] => 2
[name] => swag3
[description] => haha
[price] => 19.95
[proceeds] => 10.00
[quantity] => 2
[attributes] => [{"id":1,"name":"Size","value":"XS","active":1},{"id":8,"name":"Color","value":"Wit","active":1}]
)
Array
(
[id] => 3
[product_id] => 3
[order_id] => 3
[name] => swag2
[description] => lol
[price] => 19.95
[proceeds] => 10.00
[quantity] => 2
[attributes] => [{"id":2,"name":"Size","value":"S","active":1},{"id":7,"name":"Color","value":"Zwart","active":1}]
)
Array
(
[id] => 4
[product_id] => 3
[order_id] => 4
[name] => swag2
[description] => lol
[price] => 19.95
[proceeds] => 10.00
[quantity] => 1
[attributes] => [{"id":2,"name":"Size","value":"S","active":1},{"id":7,"name":"Color","value":"Zwart","active":1}]
)
ソートで
foreach($orders as $key => $order){
foreach($order['orderProducts'] as $key => $value){
echo '<pre>';
print_r($value['attributes']);
echo '</pre>';
}
}
結果..
Array
(
[id] => 2
[product_id] => 4
[order_id] => 2
[name] => swag3
[description] => haha
[price] => 19.95
[proceeds] => 10.00
[quantity] => 2
[attributes] => [{"id":1,"name":"Size","value":"XS","active":1},{"id":8,"name":"Color","value":"Wit","active":1}]
)
Array
(
[id] => 3
[product_id] => 3
[order_id] => 3
[name] => swag2
[description] => lol
[price] => 19.95
[proceeds] => 10.00
[quantity] => 3
[attributes] => [{"id":2,"name":"Size","value":"S","active":1},{"id":7,"name":"Color","value":"Zwart","active":1}]
)
ソリューション が、それは、ブレードです注意してくださいフロントエンドとしてPHP。
バックエンド
$order // is the array with products
$items = [];
foreach($orders as $key => $order){
foreach($order['orderProducts'] as $op){
$i = [
'product'=> Product::findOrFail($op->product_id)->toArray(),
'attributes' =>$op->attributes,
'quantity'=>$op->quantity
];
$matchedResult = false;
$count = count($items);
for($a = 0; $a < $count; $a++){
// Items with the same product_id in the $item array
if($items[$a]['product']['id'] == $i['product']['id']){
//check if the attributes are also the same
if($items[$a]['attributes'] === $i['attributes']){
// The attributes ar ethe same so up the quantity
$items[$a]['quantity'] += $i['quantity'];
$matchedResult = true;
continue; // If its right there are no other matches
}
}
}
if($matchedResult === false){
// only push item if there is not a match.
$items[] = $i;
}
}
}
フロントエンド
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Product</th>
<th>quantity</th>
</tr>
</thead>
<tbody>
@foreach($items as $item)
<tr>
<td>{{$item['product']['name']}}
@if(count($item['attributes']) > 0) <small>
@foreach($item['attributes'] as $att)
{{$att['name']}} - {{$att['value']}}
@endforeach
</small>
@endif</td>
<td>{{$item['quantity']}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
これを行う組み込みの機能はありません。それを行う独自のループを作成する必要があります。 – Barmar
正しい方向に押す:入れ子になったループ。 'foreach($ orders = $ key1 => $ value1){foreach($ orders2 $> 2)} { – Barmar
@AbraCadaverサイズと色の属性と一致しません。 – Barmar