Probably because you are using a variable in the haystack instead of an array input. So I have added a validator the will only return true for arrays that match (and not variables) and the script is as follows:
function my_in_array( $needle, $haystack ) { if (sizeof($haystack) > 0 && is_array($haystack)) { return in_array($needle, $haystack); } else { return false; } }
Thanks for your reply. It looks like my form submission was not only leaving the array empty, it wasn't creating the array at all! This is because the form fields are a bunch of checkboxes, and if none are checked, then the array that those checkboxes point to is not created.
Your suggestion does work to check this scenario. Additionally, if we use your suggestion of "is_array($haystack)", then my "sizeof($haystack)" is redundant for this test.
So this will work just fine:
function my_in_array( $needle, $haystack )
{
if (is_array($haystack))
{
return in_array($needle, $haystack);
}
else // array is undefined
{
return false;
}
}