PHP - 过滤两个数组的最有效方法

时间:2018-05-31 07:46:04

标签: php arrays sorting filtering

我有一个问题,即过滤两个对象数组的最高效方法是什么。我有两个来自不同系统的产品阵列,我想知道从一个阵列中删除了哪些产品,然后返回已经删除的产品。

请参阅下面的当前功能,我知道它是超级慢的。

 public function checkRemove($externalProducts, $localProducts){
    //Push all the SKU codes from feed to an array();
    $arr = [];
    foreach ($externalProducts->products as $product) {
        if($product->StockNumber != null){
            array_push($arr, $product->StockNumber);
        }
    }
    //Loop through the local products 
    $productsRemove = [];
    foreach ($localProducts->products as $key => $localProduct) {
        if(in_array($localProduct->sku, $arr)){

        }else{
            array_push($productsRemove, $localProduct);
        }
    }    
    return $productsRemove;
}

$externalProducts = {
"Filter": {
    "Title": "All Products"
},
"Products": [{
        "Type": "Jacket",
        "Price": 75,
        "ExpiryDate": "2018-06-30",
        "StockNumber": "180220/003",
        "Created": "2018-02-20 12:24:06",
        "Modified": "2018-05-30 02:00:23"
    },
    {
        "Type": "Jeans",
        "Price": 150,
        "ExpiryDate": "2018-06-30",
        "StockNumber": "180221/004",
        "Created": "2017-08-10 15:11:44",
        "Modified": "2018-05-30 02:00:22"
    },
    {
        "Type": "Jacket",
        "Price": 240,
        "ExpiryDate": "2018-06-30",
        "StockNumber": "150804/012",
        "Created": "2015-08-04 17:03:42",
        "Modified": "2018-05-30 02:00:22"
    }
    ]
}
$internalProducts = "localProducts": [{
    "title": "Fur Coat",
    "id": 16526,
    "created_at": "2018-05-17T10:15:45Z",
    "updated_at": "2018-05-17T10:15:45Z",
    "sku": "180514/001",
    "price": "75.00",
    "regular_price": "75.00",
    "categories": [
        "Jackets",
    ],
},
{
    "title": "Ripped Jeans",
    "id": 16527,
    "created_at": "2018-05-17T10:15:45Z",
    "updated_at": "2018-05-17T10:15:45Z",
    "sku": "180221/004",
    "price": "150.00",
    "regular_price": "150.00",
    "categories": [
        "Jeans",
    ],
},
{
    "title": "Leather Jacket",
    "id": 16528,
    "created_at": "2018-05-17T10:15:45Z",
    "updated_at": "2018-05-17T10:15:45Z",
    "sku": "150804/012",
    "price": "240.00",
    "regular_price": "240.00",
    "categories": [
        "Jackets",
    ],
    }
]

2 个答案:

答案 0 :(得分:0)

查看array_filter

您可以提供一个回调函数,该函数将为数组中的每个元素运行。如果回调函数返回true,则返回数组中的当前值。

答案 1 :(得分:0)

您仍然必须至少迭代一个数组。它是$localProducts。因此,对于$localProducts,没有任何改进。但是你可以改进$externalProducts - 添加一个只返回StockNumbers的特殊方法(如果可以的话)。如果StockNumbers的结构为:

,则更有效
[
    'stocknumber1' => true,
    'stocknumber2' => true,
    'stocknumber3' => true,
    'stocknumber4' => true,
    'stocknumber5' => true,
]

这会改善您的搜索,因为检查isset($StockNumbers['stocknumber4'])in_arrayarray_search 更快

如果您无法更改$externalProducts->products的结构,请在循环中构建库存数组:

public function checkRemove($externalProducts, $localProducts){
    //Push all the SKU codes from feed to an array();
    $arr = [];
    foreach ($externalProducts->products as $product) {
        if ($product->StockNumber != null){
            // Again I add sku as key, not as value
            $arr[$product->StockNumber] = true;
        }
    }

    //Loop through the local products 
    $productsRemove = [];
    foreach ($localProducts->products as $localProduct) {
        // check with `isset` is faster
        if (isset($arr[$localProduct->sku])) {
            array_push($productsRemove, $localProduct);
        }
    }    
    return $productsRemove;
}