脚本宝典收集整理的这篇文章主要介绍了php – 将数组值与同一数组中的其他值进行比较,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
电流回路
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>
$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 ]; } }
以上是脚本宝典为你收集整理的php – 将数组值与同一数组中的其他值进行比较全部内容,希望文章能够帮你解决php – 将数组值与同一数组中的其他值进行比较所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。