Search code examples
phparraysunset

PHP - Search if values are in array


I have these arrays:

$array = array();
array_push($array, array("id" => 1, "param" => "abc"));
array_push($array, array("id" => 2, "param" => "def"));
array_push($array, array("id" => 3, "param" => "ghi"));

[{"id":1,"param":"abc"},{"id":2,"param":"def"},{"id":3,"param":"ghi"}]


$search = array(1, 2);

I need to remove the object, making a search, if the $array contains $search value.

The final array should go like this:

[{"id":3,"nom":"ghi"}]

Any ideas? Thank you.


Solution

  • $arr = array_filter($array, function($obj) use($search) {
      return !in_array($obj['id'], $search);
    });
    

    Depending on how you are using the new array, you may have to re-index it.