in_array() expects parameter 2 to be array - when it is!

I have a warning in my log and there loads of them and I’m trying to figure out how to resolve it, but I cant work it out.

PHP Warning:  in_array() expects parameter 2 to be array, string given in

<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" />

This is where the array is put together -

if (!empty($_GET['countries']) ? $_GET['countries'] : null) {
 $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 .' )'; 
}

Any suggestions?

Try var_dump( $_GET[‘countries’]) ) before the foreach statement. I think it will be a string and not an array().

Off Topic:

pecked from a mobile

Ok this is what I get.

when none of the checkboxes are clicked I don’t get any feedback, which is fine, when I click the first checkbox I get

array(1) { [0]=> string(1) "9" }

Then when I click the second checkbox to increase the search

array(2) { [0]=> string(1) "9" [1]=> string(2) "86" }

And so on the clicking the third

array(3) { [0]=> string(1) "9" [1]=> string(2) "86" [2]=> string(2) "68" } 

And then forth checkbox

array(4) { [0]=> string(1) "9" [1]=> string(2) "86" [2]=> string(2) "68" [3]=> string(1) "5" }

It says string, but I’m building an array, I don’t get it in honesty

“I’m building an array”

But… the array you’re building isnt being used to do the in_array?

in_array($q[‘Id_Cntry’], $_REQUEST[‘countries’]))

$_REQUEST[‘countries’] comes from a form input; this form seems to use checkboxes with values, and i’m gonna guess it’s action is set to POST… which your function isnt checking for, it’s looking at GET.

That would be my guess.

Ok… (your post was being written as i made mine :P)
If you’ve put var_dump($_REQUEST[‘countries’]) right before the line that generates the checkboxes and gotten that result, then i’m as confused as you…

Probably going to need to see more of your code to spot the gotchas.

I have moved the var_dump as shown below, and there a before, during and after result with the checkboxes.

So this is the code -

$r=mysql_query("SELECT DISTINCT(Id_Cntry), Nom_Cntry_DE FROM tbl_countries LEFT JOIN tbl_hotels     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') ORDER BY tbl_countries.Nom_Cntry_DE");

while($q=mysql_fetch_assoc($r)){ ?>
<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']?>

<?php var_dump($_REQUEST['countries']); ?>

And this is what I get before the checkbox is selected -

 string(0) ""

When I select the checkbox -

array(1) { [0]=> string(1) "9" }

And then the same again after I de-select that checkbox -

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

So there something going on there

I’m thinking in the line below, that I possibly need an !isset maybe, but I’m not sure hot to fit it in

<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" />

I’m wondering if the javascript could be causing a problem, do you have firebug or en equivalent console browser plugin installed to be able to see what is being sent by the browser?

btw, are you 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

Actually PHP 5.6 is the current version at the time I am writing this although PHP 7 is scheduled to become the current version some time today (correction: it hasn’t been announced anywhere I can see yet but PHP 7.0.0 GA can be downloaded from https://github.com/php/php-src/releases/tag/php-7.0.0 so obviously it already is the current version).

The mysql_ interface HAS been removed from PHP 7 and so now only exist in old versions of PHP.

1 Like

Hi SpacePhoenix,

Yes I am finding it strange too, and am using debugger with firefox and still cant see anything.

Here is the link to the page where I am testing it out on -

http://www.checksafetyfirst.de/category_Result.php?Category=Besonsere%20Bedurfnisse&countries=

The chechboxes on the left are also outputting the var_dump we talked about earlier.

I’m not surprised you get the error message when there are no checkboxes selected, because at that point the array does not exist. I thought the problem was that you were getting that error even though the array was being passed correctly.

You check for it existing with an isset, but surely you’ll need to put the next part (the in_array() call) inside that if? Using it as the second half of an AND check means that it will run the in_array even if the isset() fails, it has to in order to evaluate whether both are true. Maybe something like this, but I might have imbalanced brackets.

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

Thanks droopsnoot, I put it in and it worked but the undefined index error is still there.

Is that definitely coming from that line, or is the var_dump($_REQUEST[‘countries’]) throwing the error now?

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