如何使用另一个具有多个值的数组从数组中删除值?

时间:2018-12-24 07:16:17

标签: php arrays

我有两个数组Array1和Array2,我需要从Array1中删除Array2的值,我在这里显示两个数组。

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

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。

4 个答案:

答案 0 :(得分:3)

仅出于娱乐目的(我有点被遗忘了:()。按ID对两个数组进行索引(array_column()需要php 7+支持对象作为输入),然后array_diff_key()从第二个数组中删除任何内容...

print_r(array_diff_key(array_column($array2, null, "id"), 
                  array_column($array1, null, "utype_id")));

我想说一个foreach()解决方案比这更快,只是想加入并发布一些原始内容。

答案 1 :(得分:2)

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

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

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

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

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

答案 2 :(得分:1)

使用嵌套循环执行资格检查。最佳做法是使用break以避免不必要的迭代。

代码:(Demo

$blacklist = [
    (object)["id" => 22,"accessid" => 2, "utype_id" => 11, "discount" => 3434, "published" => 1],
    (object)["id" => 23,"accessid" => 2, "utype_id" => 14, "discount" => 2, "published" => 1]
];

$rows = [
    (object)["id" => 9, "type_name" => "Admin", "description" => "admin", "published" => 0],
    (object)["id" => 10, "type_name" => "Senior sales", "description" => "senior sales", "published" => 0],
    (object)["id" => 11, "type_name" => "junior sales", "description" => "junior sales", "published" => 1],
    (object)["id" => 14, "type_name" => "dealer", "description" => "dealer", "published" => 0],
    (object)["id" => 15, "type_name" => "fgdg", "description" => "dfg", "published" => 1],
    (object)["id" => 16, "type_name" => "fgdfg", "description" => "fgdfg", "published" => 0]
];

foreach ($blacklist as $disqualifier) {                 // iterate the blacklist
    foreach ($rows as $index => $row) {                 // iterate the list to be checked
        if ($row->id === $disqualifier->utype_id) {     // if row should be disqualified
            unset($rows[$index]);                       // remove the row
            break;                                      // stop checking the $rows for this $disqualifier
        }
    }
}

var_export($rows);

...如果需要重新索引输出,可以调用array_values($rows)

如果这些对象数组来自数据库表,则应改进查询以提前执行此过滤过程。

答案 3 :(得分:0)

可以使用。.

$arr1ids = array();
foreach($array1 as $val1){
    $arr1ids[] = $val1->utype_id;
}

$resArr = array();
foreach($array2 as $val2){
    if(!in_array($val2->utype_id,$arr1ids)){
       $resArr[] = $val2;
    }  
}

print_r($resArr);