我在多选框中使用的数组内函数出错

时间:2009-12-11 06:08:51

标签: arrays function warnings

我在编辑表单中使用了多个选择框,使用“in-array($ needle,$ haystack)”php函数在其中显示以前选择的项目,但是如果变量数组“$ haystack”中存储了多个值,没有价值,在那里我得到警告信息提示第二个参数的错误数据类型为“in-array($ needle,$ haystack)”

我的选择框代码如下:

**<select name="otherdiseases[]" id="otherdiseases" class="list-menu"  multiple="multiple">
               <option value=''>Other Medical Diseases</option>
                <option value='Prior heart Attack'<?php if(count($res_user[0]['otherdiseases'])!=0){ if(in_array('Prior heart Attack',$res_user[0]['otherdiseases'])){ ?> selected="selected" <?php } }?>>Prior heart Attack</option>

                <option value='Ischemic Heart Attack'<?php if(count($res_user[0]['otherdiseases'])!=0){ if(in_array('Ischemic Heart Attack',$res_user[0]['otherdiseases'])){ ?> selected="selected" <?php } }else{ ?> selected=""<?php }?>>Ischemic Heart Attack</option>

                <option value='Stroke' <?php if(count($res_user[0]['otherdiseases'])!=0){ if(in_array('Stroke',$res_user[0]['otherdiseases'])){ ?> selected="selected" <?php } }?> >Stroke</option>

                <option value='Dialysis' <?php if(count($res_user[0]['otherdiseases'])!=0){ if(in_array('Dialysis',$res_user[0]['otherdiseases'])){ ?> selected="selected" <?php } }?> >Dialysis</option>

                    <option value='High BP' <?php if(count($res_user[0]['otherdiseases'])!=0){ if(in_array('High BP',$res_user[0]['otherdiseases'])){ ?> selected="selected" <?php } }?> >High BP</option>

                <option value='High Cholesterol' <?php if(count($res_user[0]['otherdiseases'])!=0){ if(in_array('High Cholesterol',$res_user[0]['otherdiseases'])){ ?> selected="selected" <?php } }?> >High Cholesterol</option>

                <option value='Thyroid' <?php if(count($res_user[0]['otherdiseases'])!=0){ if(in_array('Thyroid',$res_user[0]['otherdiseases'])){ ?> selected="selected" <?php } }?> >Thyroid</option>

                <option value='Previous Eye Laser Therapy' <?php if(count($res_user[0]['otherdiseases'])!=0){ if(in_array('Previous Eye Laser Therapy',$res_user[0]['otherdiseases'])){ ?> selected="selected" <?php } }?> >Previous Eye Laser Therapy</option>
            <option value='Foot Surgeries' <?php if(count($res_user[0]['otherdiseases'])!=0){ if(in_array('Foot Surgeries',$res_user[0]['otherdiseases'])){ ?> selected="selected" <?php } }?> >Foot Surgeries</option>
        </select>**

在上面的代码中,变量数组$ res_user [0] ['otherdiseases']是从select查询中提取的,我已经使用var_dump交叉检查了它的值存在...

当$ res_user [0] ['otherdiseases']中有0值时,请帮助我纠正我的情况..以避免上述警告......

1 个答案:

答案 0 :(得分:0)

在使用is_array

之前,使用in_array检查数组值
if (is_array($res_user[0]['otherdiseases']) && 
    in_array('needle', $res_user[0]['otherdiseases']))
{
 // Do Something
}