user3322610
user3322610

Reputation: 39

Checking session array for certain value

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 ) ) )

Upvotes: 0

Views: 48

Answers (1)

Satish Sharma
Satish Sharma

Reputation: 9635

make it correct

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

to

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

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

Upvotes: 1

Related Questions