What I'm trying to achieve is that it will go through an array. It will then look if the elements of the array are the same at three points: product_id, size value and color value. I want to create a new array that lists the elements, the only thing I don't want is duplicate values. I want duplicate values if they are the same at those three points that the quantity will be counted together. For example, if I have 3 elements of the same product identifier of the same size and the same color, and both of the three I ordered 3 elements in my new array, it’s just 1 time, and the quantity will be 9. Therefore, in my new duplicate value I don’t there will be an array.
Current loop
foreach($orders as $key => $order){ foreach($order['orderProducts'] as $key => $value){ echo '<pre>'; print_r($value['attributes']); echo '</pre>'; } }
leads to the next array
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}] )
Regarding what I'm looking for ..
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}] )
Solution Please note that php blade is an interface.
Backend
$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; } } }
Frontend
<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>