我想要实现的是,它将循环通过数组.然后,它将查看数组中的项是否在三个点上相同:product_id,大小值和颜色值.
我想创建一个列出项目的新数组,我唯一不想要的是重复的值.我想要重复的值,如果它们在数量将一起计数的那三个点上是相同的.就像我有3个相同产品ID同样大小和相同颜色的三个项目我在新阵列中订购了3个项目时,我只是站了一次,数量将是9.因此我的新项目中没有重复的值阵列.
我想创建一个列出项目的新数组,我唯一不想要的是重复的值.我想要重复的值,如果它们在数量将一起计数的那三个点上是相同的.就像我有3个相同产品ID同样大小和相同颜色的三个项目我在新阵列中订购了3个项目时,我只是站了一次,数量将是9.因此我的新项目中没有重复的值阵列.
电流回路
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] => 2 [attributes] => [{"id":2,"value":"S",{"id":7,"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,"active":1}] )
我要找的是什么..
Array ( [id] => 2 [product_id] => 4 [order_id] => 2 [name] => swag3 [description] => haha [price] => 19.95 [proceeds] => 10.00 [quantity] => 2 [attributes] => [{"id":1,"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,"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>
您可以在不使用嵌套循环的情况下实现目标.您可以使用product_id,size和color参数的哈希函数,并将该值用作新的数组键,如下所示:
$orders = // original array; $newOrders = []; // new array foreach($orders as $order) { $pi = $order["product_id"]; // get product_id $attr = json_decode($order["attributes"]); // get attributes: $size = $attr[0]->value; // get size value $color = $attr[1]->Color; // get color $hash = sprintf("%s.%s.%s",$pi,$size,$color); // Calculate hash if ($newOrders[$hash]) { $newOrders[$hash].quantity++; // If hash is already present then just increase quantity } else { // Otherwise add new order $newOrders[$hash] = [ "order" => $order,"quantity" => 1 ]; } }