【问题标题】:Merge a Laravel array of Collections, and add new properties/attributes [closed]合并一个 Laravel 集合数组,并添加新的属性/属性 [关闭]
【发布时间】:2020-08-21 10:15:43
【问题描述】:

我有一个“产品”集合数组,如下面的转储所示。

Illuminate\Support\Collection {#918 ▼
    #items: array:12 [▼
      0 => App\Product {#934 ▶}
      1 => App\Product {#943 ▶}
      2 => App\Product {#959 ▶}
      3 => App\Product {#968 ▶}
      4 => App\Product {#984 ▶}
      5 => App\Product {#993 ▶}
      6 => App\Product {#1009 ▶}
      7 => App\Product {#1018 ▶}
      8 => App\Product {#1034 ▶}
      9 => App\Product {#1043 ▶}
      10 => App\Product {#1059 ▶}
      11 => App\Product {#1068 ▶}
    ]
  }

这个数组只有两个独特的产品;

App\Product {#934 ▼
   ...
  +wasRecentlyCreated: false
  #attributes: array:10 [▼
    "id" => 1
    "slug" => "26027686555545"
    "details" => App\DispatchStock {#900 ▼
        ...
      #attributes: array:9 [▼
        ...
        "quantity" => 3
        "created_at" => "2020-05-04 15:56:07"
      ]
      ...
    }
  ]
...
}

App\Product {#943 ▼
    ...
    +wasRecentlyCreated: false
    #attributes: array:10 [▼
      "id" => 2
      "slug" => "26027687444410"
      "details" => App\DispatchStock {#899 ▼
        ...
        #attributes: array:9 [▼
          ...
          "quantity" => 5
          "created_at" => "2020-05-04 15:56:07"
        ]
        ...
      }
    ]
    ...
  }

每个重复 6 次(具有不同的“数量”)。
我想遍历“Products”的集合数组,合并所有重复项,并返回一个仅包含唯一产品的数组。
并且有类似的东西:

Illuminate\Support\Collection {#918 ▼
    #items: array:2 [▼
      0 => App\Product {#934 ▶}
      1 => App\Product {#943 ▶}
    ]
  }

在将新属性“combined_available_quantity”(产品的所有“数量”之和)附加到每个唯一产品的“详细信息”属性时。 例如,我希望最终的“产品”之一看起来像:

App\Product {#943 ▼
    ...
    +wasRecentlyCreated: false
    #attributes: array:10 [▼
      "id" => 2
      "slug" => "26027687444410"
      "details" => App\DispatchStock {#899 ▼
        ...
        #attributes: array:9 [▼
          ...
          "quantity" => 5
          "created_at" => "2020-05-04 15:56:07"
          "combined_available_quantity" => 27
        ]
        ...
      }
    ]
    ...
  }

我已经开始使用下面的代码了。

...
$combinedProducts = collect();
foreach ($products as $product) {

    $combinedProducts = $combinedProducts->each(function($combinedProduct) use ($combinedProducts, $product) {
                            if ($combinedProduct->slug == $product->slug) {
                                $qty = $product->details->quantity + $combinedProduct->details->quantity;
                                $combinedProduct->details->setAttribute('combined_available_quantity', $qty);
                            } else {
                                $product->details->setAttribute('combined_available_quantity', $product->details->quantity);
                                $combinedProducts->push($product);
                            }
                        });

    $combinedProducts = $combinedProducts->values();
}

但它有问题,并没有给我所需的结果。
我需要更好的方法。谢谢。

【问题讨论】:

  • 那为什么不修复那个代码呢?不知道为什么会失败,就不可能提供帮助
  • @NicoHaase,由于我如何实现这些循环,我显然没有得到所需的结果。我知道这一点,但没有足够的时间单独继续调试。这就是为什么我要求“更好的方法”。

标签: php arrays laravel collections


【解决方案1】:
$collect->groupBy('slug')->map(function($item){
    return $item->sum('quantity');
})

【讨论】:

  • 谢谢拉米兹。在此之前,我还没有真正探索过“groupBy”和“map”函数的使用。它们正是我所需要的。我已经编辑了您的答案,以展示我是如何解决我的问题的。
猜你喜欢
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 2014-05-12
  • 1970-01-01
  • 2019-09-19
  • 2014-03-10
  • 1970-01-01
相关资源
最近更新 更多