Home >Backend Development >PHP Tutorial >In PHP, how do I check in the most efficient way if a specific value exists in an array containing thousands of values?

In PHP, how do I check in the most efficient way if a specific value exists in an array containing thousands of values?

PHPz
PHPzforward
2023-09-09 08:13:081020browse

In PHP, how do I check in the most efficient way if a specific value exists in an array containing thousands of values?

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, you can customize it as follows−

function array_keys_exists(array $keys, array $arr) {
   return !array_diff_key(array_flip($keys), $arr);
}

The above is the detailed content of In PHP, how do I check in the most efficient way if a specific value exists in an array containing thousands of values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete