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

Can someone help with this error please with the line of code below

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

The second parameter in the function is:-

$q['Id_Rsrt']

Which is an element from an array, not necessarily an array itself.
You also seem to have a third parameter, the optional third parameter should be a boolean.

Umm ok,

I’m still a little unsure, as we are currently moving over to mysqli, this is the full block of code

$r=mysqli_query($con,"SELECT DISTINCT(tbl_resorts.Id_Rsrt), Nom_Rsrt, IdCntry_Rsrt, Id_show, Act_Hot, IdRsrt_Hot FROM tbl_resorts LEFT JOIN tbl_hotels ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) WHERE (tbl_resorts.IdCntry_Rsrt='".$selectCountry."') AND (tbl_hotels.Act_Hot='1') ORDER BY Nom_Rsrt");

while($q=mysqli_fetch_assoc($r)){ ?>

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

It seems you have edited your code, so now $_REQUEST['regions'] is the second parameter in the function which is expected to be an array. Are you still getting the same error, is “regions” an array?

Hi SamA74,

Ye it is an array, and the array by the looks comes from here -

if (!empty($_GET['regions']) ? $_GET['regions'] : null) {
//if (is_array($_GET['regions'])) {
$regionsArray = array();
foreach($_GET['regions'] as $regions) {     
$regionsArray[] = '\''.$regions.'\'';
}

So I’m not sure what i can change it to to stop the warning

Can you use $regionsArray as the array, rather than the $_REQUEST['regions'] array, if the code you posted is run before the loop?

Ah ye, you got it. Changed them and it worked

Thanks

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