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

时间:2021-09-06 21:28:38
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";
}

Guys I have the function below to check my cart array to see whether english subject exist or not, but the problem is even when english isn't in the cart array it will still return yes result, why is that so?

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

Below are the sample cart array.

以下是示例购物车阵列。

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

1 个解决方案

#1


1  

make it correct

使它正确

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

to

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

UPDATE 2 :

更新2:

try your modified function

尝试修改后的功能

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;   
}

#1


1  

make it correct

使它正确

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

to

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

UPDATE 2 :

更新2:

try your modified function

尝试修改后的功能

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;   
}