1

I have one multidimensional array:

Array
(
    [0] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1201
        )

    [1] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1200
        )

    [2] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1196
        )

    [3] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1193
        )
)

and I want to remove array which values are match with property_id:

Array
(
    [0] => 1201
    [1] => 1200
    [2] => 1193
)

and I want this result:

Array
(
    [0] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1196
        )
)

I am sharing my code for what i have done:

for($b=0; $b<count($beBounceResults); $b++){
    $beBounceProID[] = $beBounceResults[$b]->property_id;
}
// Getting thus array in this variable $beBounceProID
Array
(
    [0] => 1201
    [1] => 1200
    [2] => 1193
)

$counter = "0";
foreach ($results as $key => $value){
    if($results[$key]->property_id == $beBounceProID[$counter]){
        unset($results[$key]);
    }
    $counter++;
}

but after that I am getting Notice: Undefined offset:

Any idea what I am doing wrong.

Thanks.

4
  • instead of $counter = "0"; use $counter = 0; Commented Apr 8, 2015 at 8:26
  • You have 1193 in your list, but still want to keep it?! Commented Apr 8, 2015 at 8:26
  • @anantkumarsingh This makes no difference! Absolutely wrong, if it 's index 0 it will cast it implicit to an int Commented Apr 8, 2015 at 8:26
  • Sorry guys its my mistake. I have updated my question. Commented Apr 8, 2015 at 8:28

2 Answers 2

5

try this

foreach ($results as $key => $value){
    if(in_array($results[$key]->property_id , $beBounceProID) )
    {
        unset($results[$key]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Fast and simple to understand +1
2

Another approach is just to filter the arrays which you don't want out like this:

$arr = array_filter($arr, function($v)use($property_id){
    return !in_array($v->property_id, $property_id);
}); 

Comments

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.