proggR Posted May 31, 2011 Share Posted May 31, 2011 I'm just wondering if there is a function like array_key_exists that could take an array as the search parameter and check that all keys exist. It would be easy enough to code if I need to but I was hoping it would already exist. All wanted to use it for is a really quick form check to make sure that all the expected values were POSTed before I actually start filtering and validating individual fields. Thanks in advance for any recommendations Quote Link to comment https://forums.phpfreaks.com/topic/237937-array_key_exists-using-array-for-search-parameter/ Share on other sites More sharing options...
requinix Posted May 31, 2011 Share Posted May 31, 2011 No function that I know of. You could do it with a rather long one-liner, but a simple loop would be easier to deal with. Quote Link to comment https://forums.phpfreaks.com/topic/237937-array_key_exists-using-array-for-search-parameter/#findComment-1222682 Share on other sites More sharing options...
mikesta707 Posted May 31, 2011 Share Posted May 31, 2011 I'm pretty positive no such function exists, but as you said, using the built in array_key_exists() function iteratively or recursively would be easy enough to do what you want Quote Link to comment https://forums.phpfreaks.com/topic/237937-array_key_exists-using-array-for-search-parameter/#findComment-1222683 Share on other sites More sharing options...
joel24 Posted May 31, 2011 Share Posted May 31, 2011 you could use array_diff()... i.e. $array1 = array("green", "yellow", "red"); $array2 = array("green", "yellow", "blue", "red"); $result = array_diff($array1, $array2); if (empty($result)) { echo "found"; } else { echo "not found"; } Quote Link to comment https://forums.phpfreaks.com/topic/237937-array_key_exists-using-array-for-search-parameter/#findComment-1222684 Share on other sites More sharing options...
mikesta707 Posted May 31, 2011 Share Posted May 31, 2011 well since he's trying to see if a set of array keys exists in an array, he'd probably need to use array_diff in combination with array keys. like $subjectArray = array("array we are looking through", ...); $searchKeys = array ("keys you are looking for", ...); $result = array_diff($searchKeys, array_keys($subjectArray)); if (empty($result)){ //all the keys in searchKeys are defined in the subjectArray } Quote Link to comment https://forums.phpfreaks.com/topic/237937-array_key_exists-using-array-for-search-parameter/#findComment-1222687 Share on other sites More sharing options...
joel24 Posted May 31, 2011 Share Posted May 31, 2011 whoops, misread that. substitute array_diff() with array_diff_key() Quote Link to comment https://forums.phpfreaks.com/topic/237937-array_key_exists-using-array-for-search-parameter/#findComment-1222696 Share on other sites More sharing options...
proggR Posted May 31, 2011 Author Share Posted May 31, 2011 Awesome. Thanks for that Quote Link to comment https://forums.phpfreaks.com/topic/237937-array_key_exists-using-array-for-search-parameter/#findComment-1222716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.