简体   繁体   English

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

[英]How to Remove value from an array using another array with multiple values?

I have two array Array1 and Array2 and I need to remove Array2 value from Array1 I show my both the Array here. 我有两个数组Array1和Array2,我需要从Array1中删除Array2的值,在这里我将同时显示两个Array。

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中,我的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
        )

)

I didn't get any solution for this. 我对此没有任何解决方案。 I need only 9,10,15,16 Record id from Array2. 我只需要Array2中的9,10,15,16记录ID。

Just for entertainment purposes (and I was feeling a bit left out :( ). Index both arrays by the ID (need php 7+ for array_column() to support objects as input) and then array_diff_key() to remove any from the second array... 仅出于娱乐目的(我感觉有点被遗忘了:()。按ID对两个数组进行索引(需要php 7+来支持array_column()以支持对象作为输入),然后使用array_diff_key()从第二个数组中删除任何数组...

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

I would like to say that a foreach() solution is faster than this, just wanted to join in and post some original content. 我想说一个foreach()解决方案比这要快,只是想加入并发布一些原始内容。

First, extract utype_id s from first array, make them keys to speed up search: 首先,从第一个数组中提取utype_id ,使其成为以加快搜索速度:

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

Then, filter second array using $utype_ids : 然后,使用$utype_ids过滤第二个数组:

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

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

Use nested loops to perform the qualifying checks. 使用嵌套循环执行合格检查。 Use a break as a matter of best practice to avoid unnecessary iterations. 作为最佳实践,请使用break ,以避免不必要的迭代。

Code: ( 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);

...if you need the output to be reindexed, you can call array_values($rows) . ...如果需要重新索引输出,则可以调用array_values($rows)

If these arrays of objects are coming from a database table, you should be improving your query to do this filtration process in advance. 如果这些对象数组来自数据库表,则应改进查询以提前执行此过滤过程。

You can use.. 您可以使用..

$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);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM