This is the function I wrote for myself to use within a class.
<?php/** * Check the keys of an array against a list of values. Returns true if all values in the list is not in the array as a key. Returns false otherwise. * * @param $array Associative array with keys and values * @param $mustHaveKeys Array whose values contain the keys that MUST exist in $array * @param &$missingKeys Array. Pass by reference. An array of the missing keys in $array as string values. * @return Boolean. Return true only if all the values in $mustHaveKeys appear in $array as keys. */ function checkIfKeysExist($array, $mustHaveKeys, &$missingKeys = array()) { // extract the keys of $array as an array $keys = array_keys($array); // ensure the keys we look for are unique $mustHaveKeys = array_unique($mustHaveKeys); // $missingKeys = $mustHaveKeys - $keys // we expect $missingKeys to be empty if all goes well $missingKeys = array_diff($mustHaveKeys, $keys); return empty($missingKeys); }$arrayHasStoryAsKey = array('story' => 'some value', 'some other key' => 'some other value');$arrayHasMessageAsKey = array('message' => 'some value', 'some other key' => 'some other value');$arrayHasStoryMessageAsKey = array('story' => 'some value', 'message' => 'some value','some other key' => 'some other value');$arrayHasNone = array('xxx' => 'some value', 'some other key' => 'some other value');$keys = array('story', 'message');if (checkIfKeysExist($arrayHasStoryAsKey, $keys)) { // return false echo "arrayHasStoryAsKey has all the keys<br />";} else { echo "arrayHasStoryAsKey does NOT have all the keys<br />";}if (checkIfKeysExist($arrayHasMessageAsKey, $keys)) { // return false echo "arrayHasMessageAsKey has all the keys<br />";} else { echo "arrayHasMessageAsKey does NOT have all the keys<br />";}if (checkIfKeysExist($arrayHasStoryMessageAsKey, $keys)) { // return false echo "arrayHasStoryMessageAsKey has all the keys<br />";} else { echo "arrayHasStoryMessageAsKey does NOT have all the keys<br />";}if (checkIfKeysExist($arrayHasNone, $keys)) { // return false echo "arrayHasNone has all the keys<br />";} else { echo "arrayHasNone does NOT have all the keys<br />";}
I am assuming you need to check for multiple keys ALL EXIST in an array. If you are looking for a match of at least one key, let me know so I can provide another function.
Codepad here http://codepad.viper-7.com/AKVPCH