检查会话数组是否有特定值

时间:2014-02-25 10:02:40

标签: php arrays foreach

function CHECKITEMEXIST($cartarray, $sub){
    foreach ($cartarray as $item){
        foreach ($item as $item2){
            if($item2['subject'] = $sub){
                return '1';

            }else{
                return '0';
            }
        }
    }
}

$subject = "English";
    $checkitemexist = CHECKITEMEXIST($cart, $subject);

if($checkitemexist > 0){
    echo "Yes";
}else{
    echo "No";
}

伙计我有以下功能检查我的购物车阵列,看是否存在英语主题,但问题是即使英语不在购物车数组中,它仍然会返回是结果,为什么会这样?< / p>

以下是样本购物车数组。

Array ( [0] => Array ( [0] => Array ( [subject] => science ) ) )

1 个答案:

答案 0 :(得分:1)

使其正确

if($item2['subject'] = $sub){   // = is an assignment operator

if($item2['subject'] == $sub){  // == is a comparison operator

更新2:

尝试修改后的功能

function CHECKITEMEXIST($cartarray, $sub){
$flag = 0;
    foreach ($cartarray as $item){
        foreach ($item as $item2){
            if($item2['subject'] == $sub){
                $flag = 1;
            break;
            }
        }
        if($flag==1)
        {
            break;
        } 
    }
return $flag;   
}