phpif-statementnumbersexact-match

Check IF Exact Number Exists PHP


Inside a JSON file I have multiple keys with the name type and their values are numeric. What I'm trying to do is to check if the exact number exists. The problem is that if have two values with the same digit it shows me both TRUE. Eg 41 & 1.

What I tried so far

$regex = '/^1$/';

foreach ($value['events'] as $event) { 
    if ($event['type'] == $regex) {
       echo 'Exist';
    }     
}  

Thank you


Solution

  • You can use array_column() and in_array()

    if (in_array(1, array_column($value['events'], 'type'))) {
        echo "Exist";
    }