Home > OS >  How to check if in multidimensional array exist multiple key value
How to check if in multidimensional array exist multiple key value

Time:03-22

I have to check that some value exist in multidimensional array.

For exmaple I have array:

Array (
 [0] => Array (
 [reservation_start] => 12:05:00 [reservation_end] => 12:40:00 )
 [1] => Array ( [reservation_start] => 13:15:00 [reservation_end] => 13:50:00
 ) 

And I have both values of subarray like: $reservation_start = 13:15:00, and $reservation_end = 13:50:00 How to check whether both values are exists in specific subarray?

CodePudding user response:

You can use isset() for instance:

$arr = [
   0 => ['reservation_start' => 0],
];
var_dump(isset($arr[0]['reservation_start']));

CodePudding user response:

As long as the data you're searching only contains those two keys, you can use in_array(). Make an array using your two variables, and use it as the "needle" parameter.

$target = [
    'reservation_start' => $reservation_start,
    'reservation_end' => $reservation_end
];

$found = in_array($target, $data);

There's an example in the PHP manual. (See "Example #3 in_array() with an array as needle")

I made an example you can try online here: https://3v4l.org/XRAAN

  • Related