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

Dani AI

Generated

Most likely cause: PHP is complaining because the second argument you passed to in_array() is not an array (it is null, undefined, a string, or otherwise). in_array() expects an array for the haystack. (php.net)

Common culprits in this thread: the day index you use ($working_time[date('N')]) can be undefined (so the result is null), the variable might not be in scope, or a form/POST array wasn't created when nothing was submitted (checkboxes often behave this way). and were right to suggest guarding the haystack with is_array()/checks. The trailing comma in your array literal is not the cause — PHP array syntax allows a trailing comma. (php.net)

A safe, simple pattern to use (keeps the date logic explicit and prevents the warning):

$day = (int) date('N');                   // 1..7
$hourToCheck = ((int) date('H') + 1) % 24; // 0..23 safely

$hours = (isset($working_time[$day]) && is_array($working_time[$day]))
         ? $working_time[$day]
         : [];

if (in_array($hourToCheck, $hours, true)) {
    $page_to_show = 'first.php';
}

Note: date('N') returns 1 (Monday) through 7 (Sunday) and date('H') returns "00"–"23", so casting to int and using % 24 avoids getting 24 when you add 1. (php.net)

Quick debugging tips: var_dump($working_time, $working_time[date('N')]); will show what you actually have. Turn on error reporting during dev, and if the array can come from form input, default it to an empty array ($arr = $_POST['foo'] ?? [];) before calling in_array(). Using the strict flag in in_array(..., true) avoids type-coercion surprises. (php.net)

Recommended Answers

All 5 Replies

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 Member #117553


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.