malusman 0 Newbie Poster

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;	
        }
}
malusman 0 Newbie Poster

This is a very old thread, but it was the #1 hit with my particular error.

I found that the PHP in_array() function will give the "wrong datatype for second argument" warning if the array you are checking is empty or null. In this case, it appears that PHP doesn't know the type of data for the array argument, even if you define it as an array. I even tried defining it as global and it did not help.

A quick solution for me was to create a new in_array() function that wraps the built-in function, but checks the length of the array first.

function my_in_array( $needle, $haystack ) 
{
	if (sizeof($haystack) > 0)
	{
		return in_array($needle, $haystack);
	}
	return false;	
}