1
<?php   
session_start();
 if(!empty($_SESSION["cart_item"])) {

            if(in_array($_GET['code'],$_SESSION["cart_item"])){
                echo "hiiii";
            }else{
                echo "byeee";}
        }else {
            $itemArray = array($_GET['code']=>array('pcode'=>$_GET['code']));
            $_SESSION["cart_item"] = $itemArray;
        }
?>

I'm trying to check if there is a similar item store in the $_SESSION[cart_item] using in_array but it only return me false although i make sure that the value is indeed store in $_SESSION.

As i go through some checking, since my $_SESSION has a multidimensional array, i should use array_key_exists and here is my code for this

if(array_key_exists($_GET['code'],$_SESSION["cart_item"])){
            echo "hiiii";
        }else{
            echo "byeee";}

But still it returns false. Can anyone point out my error?

btw I'm new to this and thanks in advance.

5
  • share your $_SESSION["cart_item"] array. Commented May 19, 2016 at 2:11
  • is in the else { } of my first part of codes Commented May 19, 2016 at 2:15
  • 3
    better change if(!empty($_SESSION["cart_item"])) to if(isset($_SESSION["cart_item"])) Commented May 19, 2016 at 2:16
  • check this answer: https://3v4l.org/gmu7k Commented May 19, 2016 at 2:21
  • thanks, i think i notice what i did wrong :) have a nice day Commented May 19, 2016 at 2:24

1 Answer 1

2

You need to change if(!empty($_SESSION["cart_item"])) to if(isset($_SESSION["cart_item"])). Also check for the $_SESSION that is a simple array or not, for matching with in_array, you need to have array like i assign.

As you mention _since my $SESSION has a multidimensional array, SO make sure your array is a single dimension if you are using in_array.

let the variables likes this:

$_SESSION["cart_item"] = array('1', '2', '3');
$_GET['code'] = '3';

if(isset($_SESSION["cart_item"])) { 
    if(in_array($_GET['code'], $_SESSION["cart_item"])){
        echo "hiiii";
    }else{
        echo "byeee";}
}else {
    $itemArray = array( $_GET['code'] => array('pcode'=> $_GET['code']) );
    $_SESSION["cart_item"] = $itemArray;
}

Result: hiiii

Now its time to use array_key_exists.

Lets change array:

$_SESSION["cart_item"] = array('1' => array(6, 7, 8), '2' => array(2, 4, 6), '3' => array(1, 5, 9));

if(array_key_exists($_GET['code'], $_SESSION["cart_item"])){
    echo "hiiii from key exists";
}else{
    echo "byeee";
}

Result: hiiii from key exists

Note: I think you understand how to use and where to use in_array and array_key_exists.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I think I found the problem. When i tried to save 1 value into the multidimensional array into SESSION, and i get var_dump to show me the output this is what i get array(1) { [333]=> array(1) { ["pcode"]=> string(3) "333" } }. But if i try to save more into the session it becomes this array(2) { [0]=> array(1) { ["pcode"]=> string(3) "333" } [1]=> array(1) { ["pcode"]=> string(3) "444" } } the key value did not save the value that i assign to it and instead it change to default 0,1,2....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.