简体   繁体   English

PHP-筛选两个数组的最有效方法

[英]PHP - Most efficient way to filter two arrays

I have a question on what is the most performant way to filter two arrays of objects. 我对过滤两个对象数组的最有效方式有疑问。 I have two arrays of products from different systems and i want to work out which products have been removed from one array and then return the products that have been removed. 我有两套来自不同系统的产品,我想弄清楚哪些产品已从一个阵列中删除,然后退回已删除的产品。

See the current function i have below which i know is super slow. 看到我下面的当前功能,我知道这是超级慢的。

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

Take a look at array_filter 看看array_filter

You can provide a callback function which will be run for each element in the array. 您可以提供将对数组中的每个元素运行的回调函数。 If the callback function returns true, the current value from the array is returned in the result array. 如果回调函数返回true,则在结果数组中返回数组的当前值。

You still have to iterate over one array at least. 您仍然至少必须迭代一个数组。 It is $localProducts . 这是$localProducts So, for $localProducts there're no improvements. 因此,对于$localProducts没有任何改进。 But you can improve $externalProducts - add a special method (if you can) that will return StockNumbers only. 但是您可以改善$externalProducts添加一个特殊方法(如果可以的话),该方法将仅返回StockNumbers More effective will be if StockNumbers will be of structure as: 如果StockNumbers具有以下结构,则将更有效:

[
    'stocknumber1' => true,
    'stocknumber2' => true,
    'stocknumber3' => true,
    'stocknumber4' => true,
    'stocknumber5' => true,
]

This will improve your search, as checking isset($StockNumbers['stocknumber4']) is faster than in_array or array_search . 这将改善您的搜索,因为检查isset($StockNumbers['stocknumber4'])in_arrayarray_search 更快

If you can't change structure of $externalProducts->products , than build array of stock numbers in a loop: 如果您无法更改$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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较 PHP 中对象的两个 arrays 的最有效方法 - Most efficient way to compare two arrays of objects in PHP 按顺序比较PHP中数组的最有效方法? - Most efficient way to compare arrays in PHP by order? 以数学方式添加两个多维数组的最有效方法? - Most efficient way to mathematically add two multidimensional arrays? 在javascript或php中创建重复数组的最有效(紧凑)方式? - Most efficient (compact) way to create repetitive arrays in javascript or php? 在MySQL语句中使用数组的最有效方法? - Most efficient way to use arrays in MySQL statements? 在两侧的两个数组之间查找和存储不存在的值的最佳和最有效的方法是哪一种? - Which is the best and the most efficient way to find and store non existent values between two arrays in both sides? PHP:将大型数组汇总为10个数组的最有效方法是什么? - PHP: What's the most efficient way to summarize a large array into just 10 arrays? 当子 arrays 在 php 中具有相同的键时,查询多维数组的值的最有效方法 - Most efficient way to query multi-dimensional array for value when sub arrays have identical keys in php 创建报表的最有效方法,可按日期组织许多PHP数据数组 - Most efficient way to create reports compiling many PHP data arrays organized by date 从2个匹配PHP中替换键的数组中交换值的最有效方法是什么? - What is the most efficient way to swap the values from 2 arrays matching a replacement key in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM