wordman
wordman

Reputation: 581

How to find a specific value in multidimensional $_SESSION array?

I am using a $_SESSION array to store item description, quantity and price for items placed in a shopping cart. This all works dandy, here's the beginning portion of the code:

$quantity = 1;

// add first item from shop page
if(isset($_POST['add_item']) && (!isset($_SESSION['cart']))) {
    $_SESSION['cart'] = array();
    $_SESSION['cart'][$_POST['product_description']] = array('quantity' => $quantity, 'price' => $_POST['product_price']);
    header("Location: http://website.com/cart.php");
    }

This loads the $_SESSION['cart'] array with the appropriate information and when I foreach loop it in the cart page, I get the exact contents of my cart. All of this works as desired.

So now what I want to do is search the $_SESSION['cart'] array to determine if a specific item is in the $_SESSION['cart'] array, such as "iPhone case - Black".

I have tried various forms of echo $_SESSION['cart']['product_description'] and get nothing. When I print_r the array, I get this:

Array
(
    [iPhone case - Black] => Array
        (
            [quantity] => 1
            [price] => 9.49
        )

)

Which is correct. Performing a var_dump yields the expected result:

array(1) {
  ["iPhone case - Black"]=>
  array(2) {
    ["quantity"]=>
    int(1)
    ["price"]=>
    string(4) "9.49"
  }

}

Certainly, if there is a better way to construct the array, I'm open to it.

All I want to do is find out if "iPhone case - Black" is in the $_SESSION['cart'] array and serve up the appropriate code based on the result.

I have tried if (in_array('iPhone case - Black', $_SESSION['cart'])) which yields nothing, and when I expand that to if (in_array('iPhone case - Black', $_SESSION['cart'][0])) or ...[1] or ...['product_description'] I receive the error:

Warning: in_array() expects parameter 2 to be array, null given in...

What am I missing that would simply let me check the $_SESSION['cart'] array to find the value, "iPhone case - Black"? Is my only option a foreach loop?

Many sincere thanks in advance!

UPDATE Code posting as per request from @rmmoul:

// add first item from shop page
if(isset($_POST['add_item']) && (!isset($_SESSION['cart']))) {
$_SESSION['cart'][] = array('product_description'=> $_POST['product_description'], 'quantity' => $quantity, 'price' => $_POST['product_price']);
header("Location: http://website.com/cart.php");
}

Returns this through a print_r:

Array
(
    [0] => Array
        (
            [product_description] => iPhone case - Black
            [quantity] => 1
            [price] => 9.49
        )

    [1] => Array
        (
        [product_description] => iPhone case - Black
        [quantity] => 1
        [price] => 9.49
    )

)

Upvotes: 1

Views: 116

Answers (3)

rmmoul
rmmoul

Reputation: 3217

You're not putting the value into a key called "product_description", you're naming the key after your product description. Try changing this line:

 $_SESSION['cart'][$_POST['product_description']] = array('quantity' => $quantity, 'price' => $_POST['product_price']);

To:

 $_SESSION['cart'][] = array('product_description'=> $_POST['product_description'], 'quantity' => $quantity, 'price' => $_POST['product_price']);

Then you will be able to echo the values like:

echo $_SESSION['cart'][0]['quantity'];
echo $_SESSION['cart'][0]['price'];
echo $_SESSION['cart'][0]['price'];

That should also let you have multiple products easily as $_SESSION['cart'][0], $_SESSION['cart'][1], etc.

You could then loop through the cart array to check for certain items like this:

foreach($_SESSION['cart'] as $product)
{
    if($product['product_description'] == 'black iphone')
    {
        //do something here
        echo $product['price'];
    }
}

This is untested though, and written from memory, so it might need tweaked a little to get it to run, but it's got the gist of what should work for you.

Edit:

I updated the foreach to be more useful.

Upvotes: 1

Suchit kumar
Suchit kumar

Reputation: 11859

since you are using $_POST['product_description'] as key the assigned value will become key i.e. 'iPhone case - Black'.

try this:

 if (isset($_SESSION['cart']['iPhone case - Black'])){

foreach($_SESSION['cart']['iPhone case - Black'] as $key=>$value){

echo $key."  ".$value."<br>";

}

Upvotes: 2

Stanislav Shabalin
Stanislav Shabalin

Reputation: 19278

What about isset?

if ( isset( $_SESSION['cart']['iPhone case - Black'] ) )

If it's true then $_SESSION['cart']['iPhone case - Black'] will contain the info:

array(2) {
    ["quantity"]=>
    int(1)
    ["price"]=>
    string(4) "9.49"
}

Upvotes: 1

Related Questions