0

I have a multi-dimensional array similar to this:

$arr1 = array(
        0 => array("departmentID"=>1,"userID"=>"3000001"),
        1 => array("departmentID"=>2,"userID"=>"3000002"),
        2 => array("departmentID"=>3,"userID"=>"3000003")
);

I basically need to search the array to see if a specific key/value pair exists. For example, I need to know if department ID 2 with userID 3000002 is in the array.

I've tried this code:

$key = array_search('2', array_column($arr1, 'departmentID'));
echo ("The key is: ".$key);

This works fine but it's only a search on the department ID. I need to know if the departmentID value of 2 exists with the userID value of 3000002 and I can't quite figure it out.

Would be grateful for any help!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Jason
  • 1,105
  • 3
  • 16
  • 30
  • what is your desired output? i hope this will help u https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value – devpro Jan 24 '19 at 16:00
  • @devpro - Just a boolean would be fine in this case. Just need to know if that specific key/value pair exists or not. – Jason Jan 24 '19 at 16:02

1 Answers1

4
$key = array_search(array("departmentID"=>2,"userID"=>"3000002"), $arr1);
brevis
  • 1,191
  • 10
  • 15