当元素条件为真时删除数组索引

时间:2019-05-17 11:37:03

标签: arrays filter match

我有两个数组集合

第一个是畅销产品集合

Array
(
    [period] => 2019-01-01
    [qty_ordered] => 12.0000
    [product_id] => 2080
    [product_name] => Watchadmin1
    [product_price] => 1010.0000
)
Array
(
    [period] => 2019-01-01
    [qty_ordered] => 11.0000
    [product_id] => 2086
    [product_name] => WatchAdmin88
    [product_price] => 1010.0000
)
Array
(
    [period] => 2019-01-01
    [qty_ordered] => 11.0000
    [product_id] => 2076
    [product_name] => admin3
    [product_price] => 1010.0000
)

第二个所有产品集合是     数组     (         [entity_id] => 2075         [attribute_set_id] => 4         [type_id] =>简单         [sku] => iphone max         [has_options] => 0         [required_options] => 0         [created_at] => 2019-02-12 09:08:55         [updated_at] => 2019-02-12 09:08:55         [current_user_id] => 1     )

Array
(
    [entity_id] => 2080
    [attribute_set_id] => 4
    [type_id] => simple
    [sku] => Watchadmin1
    [has_options] => 0
    [required_options] => 0
    [created_at] => 2019-02-25 10:06:20
    [updated_at] => 2019-05-03 10:59:27
    [current_user_id] => 1
)
Array
(
    [entity_id] => 2081
    [attribute_set_id] => 4
    [type_id] => simple
    [sku] => Watchadmin8
    [has_options] => 0
    [required_options] => 0
    [created_at] => 2019-02-25 14:36:21
    [updated_at] => 2019-05-03 12:49:05
    [current_user_id] => 1
)

product_id与第一个收藏夹的“ entity_id”匹配时,我要删除第一个收藏夹的所有产品(最畅销产品)。

我用嵌套的foreach做到了这一点,但是当成千上万的产品时,它并不是很好的代码 我的代码是

foreach ($sellercollection as $allProduct){
            foreach ($bestsalecollection as $bestSale){
                if($allProduct->getEntityId()==$bestSale->getProductId()){
                    $filter_product[]=$bestSale;
                }
            }
        }

print_r($ filter_product);

**Output is** 

Array
    (
        [period] => 2019-01-01
        [qty_ordered] => 11.0000
        [product_id] => 2076
        [product_name] => admin3
        [product_price] => 1010.0000
    )

php中有任何buildin函数吗?我不想使用foreach吗?

请推荐优化代码

0 个答案:

没有答案