Reputation: 39
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
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