how can i make the session hold 2 arrays...

    
$_SESSION["cart"] = array(); $id = $_POST["id"]; $_SESSION["cart"][$id] = $id;

i woukd like the id to also hold another value.

now there is just an item id stored but i would like an item id and quantity

thanks

    how could i do this...
    so i have...

    if ( !isset( $SESSION["cart"] ) ) {
    $
    SESSION["cart"] = array();
    }

    $id = $POST["id"];
    $qty = $
    POST["qty"];

    -- ????????????????????????????????????? --

      It's not like you need to store the $id as both the key and the value.
      If IDs are unique, then

      $_SESSION["cart"][$id] = $qty; 

      If you want to store more information

      $_SESSION["cart"][$id] = array('quantity'=>$qty, 'somethingelse'=>$thing); 

      And if IDs are not unique then they count as "more information":

      $_SESSION["cart"][] = array('id=>$id, 'quantity'=>$qty, 'somethingelse'=>$thing); 

      Where [] means that PHP will generate a key for you.

        ok, how can i get this to work now?
        foreach($_SESSION['cart'] as $value)
        {

          Um ... finish writing it?

          You are going to have to be a bit more explicit with your problem.

            ok.. maybe i have been a bit unclear...

            i have used this...

            if ( !isset( $_SESSION["cart"] ) ) { 
                $_SESSION["cart"] = array(); 
            } 
            
            $id = $_POST["id"]; 
            $qty = $_POST["qty"]; 
            $_SESSION["cart"][$id] = $qty; 
            

            ...to add the items id into the sessions array.

            now to get the items out af the array to display in the shopping cart page i used this but it doesnt work?...

            <?php
            foreach($_SESSION['cart'] as $value)
            {  
            ?>

              Can you show us the exact code you tried? The last snippet you posted isn't finished.

              You actually need to retrieve the keys as well (as these are the ID values), so this foreach() loop will probably help you see what's happening:

              foreach($_SESSION['cart'] as $id => $qty) {
                  // do stuff with the id and qty here
              }

              For more information on the [man]foreach/man loop and this slightly different syntax, visit the manual page for [man]foreach/man.

                EDIT-
                This was what i was looking for - thanks 🙂
                foreach($_SESSION['cart'] as $id => $qty) {

                  Write a Reply...