0

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

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
        )

)

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

6
  • Please show what have you tried so far. Commented Dec 24, 2018 at 7:18
  • $diff = array_diff($array2, $array1); Commented Dec 24, 2018 at 7:19
  • none of the items in the one array are in the other Commented Dec 24, 2018 at 7:20
  • Extract values from first array, then iterate or filter second array according to extracted values. Commented Dec 24, 2018 at 7:22
  • arrays of objects make it harder, no array_column :( Commented Dec 24, 2018 at 7:23

4 Answers 4

3

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...

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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;
}

Then, filter second array using $utype_ids:

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

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

Comments

1

Use nested loops to perform the qualifying checks. Use a break as a matter of best practice to avoid unnecessary iterations.

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).

If these arrays of objects are coming from a database table, you should be improving your query to do this filtration process in advance.

Comments

0

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

1 Comment

Code only answer are low-value on StackOverflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.