Check Existence of Value in Array Efficiently in PHP



A quick way of doing this has been shown below −

if (array_flip($set)[$value] !== null) {
   echo "something"; //take some action
}

To customize the number of keys, the function can be customized in the below manner −

function array_keys_exists(array $keys, array $arr) {
   return !array_diff_key(array_flip($keys), $arr);
}
Updated on: 2020-04-06T08:28:04+05:30

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements