如何使用另一个具有多个值的数组从数组中删除值? [英] How to Remove value from an array using another array with multiple values?

查看:99
本文介绍了如何使用另一个具有多个值的数组从数组中删除值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组Array1和Array2,我需要从Array1中删除Array2的值.

I have two array Array1 and Array2 and I need to remove Array2 value from Array1 I show my both the Array here.

在Array1中,我的utype_id是11和14,我需要从Array2中删除此id记录,所以我该怎么办?请帮帮我吗?

In Array1 I have utype_id is 11 and 14 and I need to remove this id record from Array2 so how can I do it can you please help me?

Array1(

    [0] => stdClass Object
        (
            [id] => 22
            [accessid] => 2
            [utype_id] => 11
            [discount] => 3434
            [published] => 1
        )

    [1] => stdClass Object
        (
            [id] => 23
            [accessid] => 2
            [utype_id] => 14
            [discount] => 2
            [published] => 1
        )
)


Array2
(
    [0] => stdClass Object
        (
            [id] => 9
            [type_name] => Admin
            [description] => admin
            [published] => 0
        )

    [1] => stdClass Object
        (
            [id] => 10
            [type_name] => Senior sales
            [description] => senior sales
            [published] => 0
        )

    [2] => stdClass Object
        (
            [id] => 11
            [type_name] => junior sales
            [description] => junior
            [published] => 1
        )

    [3] => stdClass Object
        (
            [id] => 14
            [type_name] => dealer
            [description] => dealer
            [published] => 0
        )

    [4] => stdClass Object
        (
            [id] => 15
            [type_name] => fgdg
            [description] => dfg
            [published] => 1
        )

    [5] => stdClass Object
        (
            [id] => 16
            [type_name] => fgdfg
            [description] => fgdfg
            [published] => 0
        )

)

我没有得到任何解决方案.我只需要Array2中的9,10,15,16记录ID.

I didn't get any solution for this. I need only 9,10,15,16 Record id from Array2.

推荐答案

首先,从第一个数组中提取utype_id,使其成为以加快搜索速度:

First, extract utype_ids from first array, make them keys to speed up search:

$utype_ids = [];
foreach ($array1 as $item) {
    $utype_ids[$item->utype_id] = 1;
}

然后,使用$utype_ids过滤第二个数组:

Then, filter second array using $utype_ids:

$filtered_array = array_filter(
    $array2, 
    function($v) use ($utype_ids) {
        return !isset($utype_ids[$v->id]);
    }
);

演示: https://3v4l.org/i2heV

这篇关于如何使用另一个具有多个值的数组从数组中删除值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆