[英]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_array
或array_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.