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

2022-01-25 00:00:00 arrays compare php

我想要实现的是,它将在数组中循环.然后它将查看数组中的项目是否在三个点上相同:product_id、size 值和 color 值.我想创建一个列出项目的新数组,我唯一不想要的是重复的值.我想要重复的值,如果它们在这三个点上相同,那么数量将一起计算.就像我有 3 件相同的产品 ID 相同的尺寸和相同的颜色,并且这三个我在我的新数组中订购了 3 件,这只是站立 1 次,数量将为 9.所以我的新数组中不会有重复的值数组.

What I’m trying to achieve is that, it will loop trough the array. Then it will look if the items in the array are the same on three points: product_id, the size value and the color value. I want to create a new array where the items are listed, the only thing I don’t want is the duplicated values. I want that the duplicated values if they are the same on those three points that the quantity will be count together. Like if I have 3 items same product id same size and same color and both of the three I ordered 3 items in my new array this is just standing 1 time and the quantity will be 9. So there will be no duplicated values in my new array.

当前循环

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,"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}]
)

我正在寻找的东西..

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.

Solution Note it's blade php as frontend.

后端

$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 参数的哈希函数,并将该值用作新的数组键,如下所示:

You can achieve your goal without using nested loops. You may use hash function of product_id, size and color parameters and use that value as a new array key like this:

$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
        ];
    }
}

相关文章