Warning in_array() expects parameter 2 to be array null given in

The code below is causing the error in the title, and cant see why, could someone have a look for me please.

while($q=mysql_fetch_assoc($r)){ ?> 
<input type="checkbox" name="countries[]" value="<?php echo $q['Id_Cntry']?>"onClick="javascript:checkRefresh()" <?php ((in_array($q['Id_Cntry'], $_REQUEST['countries'])) ? "checked=\"checked\" " : "")?> class="inline" /><?php echo $q['Nom_Cntry_DE']?>&nbsp;<?php
$query1 = mysql_query("select count(tbl_hotels.Id_Hot) as total1 FROM tbl_hotels LEFT JOIN tbl_countries ON (tbl_countries.Id_Cntry=tbl_hotels.IdCntry_Hot) LEFT JOIN tbl_hotname ON (tbl_hotname.Id_Hot=tbl_hotels.Id_Hot) LEFT JOIN tbl_amenities ON (tbl_amenities.Id_Ame=tbl_hotname.Id_Ame) WHERE (tbl_hotels.Act_Hot='1') AND (tbl_amenities.Id_Ame='806') AND (tbl_countries.Id_Cntry=". $q['Id_Cntry'] .") ORDER by tbl_hotels.Id_Hot");
$result1 = mysql_fetch_array($query1);

The line that’s causing the trouble is -

<input type="checkbox" name="countries[]" value="<?php echo $q['Id_Cntry']?>" onClick="javascript:checkRefresh()" <?php ((in_array($q['Id_Cntry'], $_REQUEST['countries'])) ? "checked=\"checked\" " : "")?> class="inline" /><?php echo $q['Nom_Cntry_DE']

The error is quite clear: the second parameter appears to not be an array.
Do a var_dump of $_REQUEST[‘countries’] and see what it contains?

note: unchecked checkboxes are not submitted.

Hi yes I can see wha’ts happening, but not sure how to deal with it.

What I need is for this bit of code to only work when the checkbox is checked, as its displaying a row of countries, and they only come into play once they are checked.


<?php echo $q['Id_Cntry']?>"onClick="javascript:checkRefresh()" <?php  echo (isset( $_REQUEST['countries']) && in_array($q['Id_Cntry'], $_REQUEST['countries']) ) ? 'checked="checked"' : '')?>

Hi the good news is that has resolved an issue I had which was that after selection the checkbox wasn’t staying checked and was skewing the results, so thank you very much.

I checked the logs and the array value NULL still appears unless the checkbox is checked, so it still shows as an error in the logs, so is there a way around that, but am well happy that we got one of the other issues resolved, cheers

check if the “countries” entry is set at all. you should always check a value for existence before you use it.

although filter functions can get you an empty array as default:

$countries = filter_input(INPUT_POST, 'countries', FILTER_VALIDATE_INT, FILTER_FORCE_ARRAY);

Hi Dormilich,

I have turned the errors on and can see the error of an empty array, and as we have a lot of these checkboxes, its creating a huge amount of errors.

This is the code again

<input type="checkbox" name="countries[]" value="<?php echo $q['Id_Cntry']?>" onClick="javascript:checkRefresh()" <?php echo (isset($_REQUEST['countries']) && in_array($q['Id_Cntry'], $_REQUEST['countries'])) ? 'checked="checked"' : ''?> class="inline" /><?php echo $q['Nom_Cntry_DE']?>

And if the checkbox isn’t selected I get this error -

Ägypten Notice: Undefined index: countries in \CSFFILES11\WEBSITES\live\new_checksafetyfirst\de\category_Result.php on line 447 NULL

But when the checkbox is selected I get -

Ägyptenarray(1) { [0]=> string(1) “9” }

So its fine when its checked, but not when it isn’t, and I know you have helped but I cant work out how to either give the array a value before its checked to not activate the error, or to somehow block the action that is causing the error, when the checkbox isn’t selected.

I have found the source if you like and have put an else statement to it, but what im not sure to do is what to put inside the else, to stop the error im getting when the checkbox isn’t checked.

if (is_array($_GET['countries'])) {
 $countriesArray = array();
 foreach($_GET['countries'] as $countries) {	 
 $countriesArray[] = '\''.$countries.'\'';
 $countriesData = implode(',', $countriesArray);	 
 $sqlcountries =  ' WHERE Id_Cntry IN ('. $countriesData .' )';
 $sqlcountries2 =  ' AND tbl_hotels.IdCntry_Hot IN ('. $countriesData .' )';
 //$sqlregionName =  'WHERE Id_Rsrt IN ('. $regionData .' )';
 //echo $sqlregionName; 
 //echo $regionData;
 //echo $sqlcountries;
} else {


Or maybe I’m delusional, I don’t know

Sorry about the updates.

I just changed

if (is_array($_GET['countries'])) {


if (!empty($_GET['countries']) ? $_GET['countries'] : null) {

And it seemed to cure one of them, am I on the right track here

So next its to apply I to

<input type="checkbox" name="countries[]" value="<?php echo $q['Id_Cntry']?>" onClick="javascript:checkRefresh()" <?php echo (isset($_REQUEST['countries']) && in_array($q['Id_Cntry'], $_REQUEST['countries'])) ? 'checked="checked"' : '' ?> class="inline" /><?php echo $q['Nom_Cntry_DE']?>

@multichild Be aware that the old mysql_* extension which you’re using is deprecated in version 5.5 of PHP (the current version) and is being removed from version 7 (the next version). You need migrate over to using either the mysqli_* extension or PDO

Thanks SpacePhoenix will add it to my list after this.

I seemed to make some progress yesterday and now need to apply the same sort of call to the checkbox area, as per my thread above, would you have any ideas?

Actually I’ve come to discover that I’m looking in the wrong place.

I suppose what I got to try and do is make this a null at a higher level, as its nothing to do with the code I’ve printed above.

As in declare it an empty array when it isn’t used, so that I don’t get ‘Undefined index: countries’

any ideas?

Trying this, but the error is still showing

if (@$_GET['countries']=="") {
echo "yep";
$countriesArray = array();
 foreach($_GET['countries'] as $countries) {	 
 $countriesArray[0] = '\''.$countries.'\'';

Ok forgot all the above, what I need to do is if $countries= isn’t in the url string, it needs to be added in

You should give priority to replacing the mysql_ calls.

PHP 7 is due for release any day now and it introduces the ?? operator which will make solving your missing/empty field problem much simpler but it also no longer supports mysql_ calls

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.