Skip to content

Instantly share code, notes, and snippets.

@omitobi
Created April 25, 2017 21:47
Show Gist options
  • Select an option

  • Save omitobi/f9822581d9b4b4e59bb24173ead60597 to your computer and use it in GitHub Desktop.

Select an option

Save omitobi/f9822581d9b4b4e59bb24173ead60597 to your computer and use it in GitHub Desktop.
arrays_keys_exists
/**
* Like php array_key_exists, this instead search if (one or more) keys exists in the array
* @param array $needles - keys to look for in the array
* @param array $haystack - the <b>Associative</b> array to search
* @param bool $all - [Optional] if false then checks if some keys are found
* @return bool true if the needles are found else false. <br>
* Note: if hastack is multidimentional only the first layer is checked<br>,
* the needles should <b>not be<b> an associative array else it returns false<br>
* The array to search must be associative array too else false may be returned
*/
public function array_keys_exists($needles, $haystack, $all = true)
{
$size = count($needles);
if($all) return count(array_intersect($needles, array_keys($haystack))) === $size;
return !empty(array_intersect($needles, array_keys($haystack)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment