All,
I have the following code:
$qry = "Select * from vendor_options order by vendor_option_name ASC";
$result = mysql_query($qry);
while($resultset = mysql_fetch_array($result)){
if(isset($_SESSION['pav_choosen_vendor_categories'])){
for($z=0;$z<$_SESSION['pav_choosen_vendor_categories'];$z++){
$sVendorId = $_SESSION['pav_vendor_categories_' . $z];
if($sVendorId==$resultset['vendor_option_id']){
$vendor_cats_choosen[] = $sVendorId;
}
}
if(in_array($resultset['vendor_option_id'],$vendor_cats_choosen)){
?>
<input type="checkbox" value="<?php echo $resultset['vendor_option_id']; ?>" class="select_vendor" name="vendor_categories[]" checked><?php echo $resultset['vendor_option_name']; ?><br>
<?php
}else{
?>
<input type="checkbox" value="<?php echo $resultset['vendor_option_id']; ?>" class="select_vendor" name="vendor_categories[]"><?php echo $resultset['vendor_option_name']; ?><br>
<?php
}
}
}
I’m trying to check to see if the value returned in the mysql_fetch_array is already in my array. Say the first value it finds in the array is in the fourth iteration of the while loop. I’ll get the following error:
Warning: in_array() expects parameter 2 to be array, null
Once it gets to a value that is in the array the rest of them work fine. Why does itgive an error for the first couple? Thanks.
It looks like you have not initialized
$vendor_cats_chosento be an array, and so if the conditionif($sVendorId==$resultset['vendor_option_id'])is not true, no elements will be appended to it, turning it implicitly into an array.Initialize it before the
whileloop. You should just about always initialize arrays before use.Now, when your
in_array()statement executes, the array may be empty, but will be a valid array.