将laravel中具有键值对的两个对象数组与特定键进行比较,并返回不匹配的键值

问题描述

我有两个数组: OrderItem:

[
    {
        "id": 4,"cancel": 0,"deleted_at": null,"created_at": "2020-08-12T10:14:01.000000Z","updated_at": "2020-08-12T10:14:01.000000Z"
    },{
          "id": 3,"created_at": "2020-08-12T10:14:56.000000Z","updated_at": "2020-08-12T10:14:56.000000Z",}
]

OrderReady:

[
    {
        "id": 2,"order_item_id": 4,"date": "1962-04-13","wages": 12,}  
]

因此,我需要检查OrderItem表中的order_item_id的OrderReady表中的ID,并从OrderItam表中返回记录,以防在OrderReady表中不存在该ID的情况。从上面的数据中,返回OrderItem表ID为3的记录,因为它没有出现在OrderReady表中。什么是最快最有效的方法

解决方法

以下是有关如何操作的两个建议。您会得到一个丑陋的方式和更漂亮的方式。

首先,我们迭代所有项目并与就绪项目匹配。如果找不到匹配项,则将该项目添加到“未准备好的项目”列表中,以供以后处理。

$orderItems = [
    ['id' => 1],['id' => 2],['id' => 3]
];

$orderReadyItems = [
    ['order_item_id' => 1],['order_item_id' => 3]
];

// Array for storing items that are not yet ready
$notReadyItems = [];

foreach($orderItems as $item) {
    $itemReady = false;
    // Iterate all ready items and attempt to find a match with item.
    foreach($orderReadyItems as $orderReadyItem) {
        if ($item['id'] === $orderReadyItem['id']) {
            $itemReady = true;
            break; // Stop iterating ready items if a match is found.
        }
    }

    // Add item to $notReadyItems array if no match is found.
    if ($itemReady === false) {
        $notReadyItems[] = $item;
    }
}

为使外观更漂亮,我们可以利用一些Collection方法。因此,我们无需迭代所有就绪商品,而是创建具有所有就绪商品ID的数组,并在orderItems的过滤器中进行检查,如下所示:

$readyItemIds  = collect($orderReadyItems)->pluck('order_item_id');
$notReadyItems = collect($orderItems)->filter(static function (array $item) use ($readyItemIds) {
    return !$readyItemIds->contains($item['id']);
});

转储$notReadyItems应该给您:

array:1 [
  0 => array:1 [
    "id" => 2
  ]
]