Hi,
I'm having the problem with this code, gives me "wrong datatype for second argument" error

here is the code:

<?
$page_to_show = 'second.php';
// array of days and hours
$working_time = array(
    1=>array(21,22,23,0,1),//monday hours
    2=>array(21,22,23,0,1),//tuesday hours
    3=>array(21,22,23,0,1),//and so on 
    4=>array(21,22,23,0,1),
    5=>array(21,22,23,0,1),
    6=>array(21,22,23,0,1),
    7=>array(11,12,13,14,21,22,23,0,1),
    );
 
if (in_array(date('H')+1,$working_time[date('N')])) {   [B]<-- error on this line[/B]
    $page_to_show = 'first.php';
}
include($page_to_show);
?>

I have no idea what kind of error it could be,
please help

Try taking out the extra "," at the last element of your $working_time array

7=>array(11,12,13,14,21,22,23,0,1), <--
Member Avatar for Rhyan


if (in_array(date('H')+1,$working_time[date('N')])) { ...

Did the removal of the , solve the problem?

Maybe it is good idea to preload the date value in a variable and then pass the variable as array identifier.

$arrayFinder = date('N');
if (in_array(date('H')+1,$working_time[$aarayFinder)) { ...

Advise how did you solve the problem.

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;	
}

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;	
}

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;	
    }
}

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;	
        }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.