php – 将数组值与同一数组中的其他值进行比较

前端之家收集整理的这篇文章主要介绍了php – 将数组值与同一数组中的其他值进行比较前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要实现的是,它将循环通过数组.然后,它将查看数组中的项是否在三个点上相同:product_id,大小值和颜色值.
我想创建一个列出项目的新数组,我唯一不想要的是重复的值.我想要重复的值,如果它们在数量将一起计数的那三个点上是相同的.就像我有3个相同产品ID同样大小和相同颜色的三个项目我在新阵列中订购了3个项目时,我只是站了一次,数量将是9.因此我的新项目中没有重复的值阵列.

电流回路

  1. foreach($orders as $key => $order){
  2. foreach($order['orderProducts'] as $key => $value){
  3. echo '<pre>';
  4. print_r($value['attributes']);
  5. echo '</pre>';
  6. }
  7. }

得到以下数组

  1. Array
  2. (
  3. [id] => 2
  4. [product_id] => 4
  5. [order_id] => 2
  6. [name] => swag3
  7. [description] => haha
  8. [price] => 19.95
  9. [proceeds] => 10.00
  10. [quantity] => 2
  11. [attributes] => [{"id":1,"name":"Size","value":"XS","active":1},{"id":8,"name":"Color","value":"Wit","active":1}]
  12. )
  13. Array
  14. (
  15. [id] => 3
  16. [product_id] => 3
  17. [order_id] => 3
  18. [name] => swag2
  19. [description] => lol
  20. [price] => 19.95
  21. [proceeds] => 10.00
  22. [quantity] => 2
  23. [attributes] => [{"id":2,"value":"S",{"id":7,"value":"Zwart","active":1}]
  24. )
  25. Array
  26. (
  27. [id] => 4
  28. [product_id] => 3
  29. [order_id] => 4
  30. [name] => swag2
  31. [description] => lol
  32. [price] => 19.95
  33. [proceeds] => 10.00
  34. [quantity] => 1
  35. [attributes] => [{"id":2,"active":1}]
  36. )

我要找的是什么..

  1. Array
  2. (
  3. [id] => 2
  4. [product_id] => 4
  5. [order_id] => 2
  6. [name] => swag3
  7. [description] => haha
  8. [price] => 19.95
  9. [proceeds] => 10.00
  10. [quantity] => 2
  11. [attributes] => [{"id":1,"active":1}]
  12. )
  13. Array
  14. (
  15. [id] => 3
  16. [product_id] => 3
  17. [order_id] => 3
  18. [name] => swag2
  19. [description] => lol
  20. [price] => 19.95
  21. [proceeds] => 10.00
  22. [quantity] => 3
  23. [attributes] => [{"id":2,"active":1}]
  24. )


请注意它的刀片PHP作为前端.

后端

  1. $order // is the array with products
  2. $items = [];
  3. foreach($orders as $key => $order){
  4. foreach($order['orderProducts'] as $op){
  5. $i = [
  6. 'product'=> Product::findOrFail($op->product_id)->toArray(),'attributes' =>$op->attributes,'quantity'=>$op->quantity
  7. ];
  8. $matchedResult = false;
  9. $count = count($items);
  10. for($a = 0; $a < $count; $a++){
  11. // Items with the same product_id in the $item array
  12. if($items[$a]['product']['id'] == $i['product']['id']){
  13. //check if the attributes are also the same
  14. if($items[$a]['attributes'] === $i['attributes']){
  15. // The attributes ar ethe same so up the quantity
  16. $items[$a]['quantity'] += $i['quantity'];
  17. $matchedResult = true;
  18. continue; // If its right there are no other matches
  19. }
  20. }
  21. }
  22. if($matchedResult === false){
  23. // only push item if there is not a match.
  24. $items[] = $i;
  25. }
  26. }
  27. }

前端

  1. <div class="table-responsive">
  2. <table class="table table-striped">
  3. <thead>
  4. <tr>
  5. <th>Product</th>
  6. <th>quantity</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. @foreach($items as $item)
  11. <tr>
  12. <td>{{$item['product']['name']}}
  13. @if(count($item['attributes']) > 0) <small>
  14. @foreach($item['attributes'] as $att)
  15. {{$att['name']}} - {{$att['value']}}
  16. @endforeach
  17. </small>
  18. @endif</td>
  19. <td>{{$item['quantity']}}</td>
  20. </tr>
  21. @endforeach
  22. </tbody>
  23. </table>
  24. </div>
您可以在不使用嵌套循环的情况下实现目标.您可以使用product_id,size和color参数的哈希函数,并将该值用作新的数组键,如下所示:
  1. $orders = // original array;
  2. $newOrders = []; // new array
  3.  
  4. foreach($orders as $order) {
  5. $pi = $order["product_id"]; // get product_id
  6. $attr = json_decode($order["attributes"]); // get attributes:
  7. $size = $attr[0]->value; // get size value
  8. $color = $attr[1]->Color; // get color
  9.  
  10. $hash = sprintf("%s.%s.%s",$pi,$size,$color); // Calculate hash
  11.  
  12. if ($newOrders[$hash]) {
  13. $newOrders[$hash].quantity++; // If hash is already present then just increase quantity
  14. } else {
  15. // Otherwise add new order
  16. $newOrders[$hash] = [
  17. "order" => $order,"quantity" => 1
  18. ];
  19. }
  20. }

猜你在找的PHP相关文章