This is old and will probably get buried, but this is my attempt.
I had an issue similar to @Ryan. In some cases, I needed to only check if at least 1 key was in an array, and in some cases, all needed to be present.
So I wrote this function:
/** * A key check of an array of keys * @param array $keys_to_check An array of keys to check * @param array $array_to_check The array to check against * @param bool $strict Checks that all $keys_to_check are in $array_to_check | Default: false * @return bool */function array_keys_exist(array $keys_to_check, array $array_to_check, $strict = false) { // Results to pass back // $results = false; // If all keys are expected // if ($strict) { // Strict check // // Keys to check count // $ktc = count($keys_to_check); // Array to check count // $atc = count(array_intersect($keys_to_check, array_keys($array_to_check))); // Compare all // if ($ktc === $atc) { $results = true; } } else { // Loose check - to see if some keys exist // // Loop through all keys to check // foreach ($keys_to_check as $ktc) { // Check if key exists in array to check // if (array_key_exists($ktc, $array_to_check)) { $results = true; // We found at least one, break loop // break; } } } return $results;}
This was a lot easier than having to write multiple ||
and &&
blocks.