makamo66 0 Posted November 11 This is the cart array: $cart = array( "1" => "2", "3" => "4", "5" => "6"); var_dump($cart); array(3) { [1]=> string(1) "2" [3]=> string(1) "4" [5]=> string(1) "6" } This is the cart items array: $cart_items = array(); $cart_items["id"] = $id; $cart_items["quantity"] = $quantity; var_dump($cart_items); array(2) { ["id"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["quantity"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } } I want the key to be "id" and the value to be "quantity" like it is for the cart array. How do I define my cart_items array using variables? Quote Share this post Link to post Share on other sites
maxxd 72 Posted November 11 (edited) You can use variables as the index when you create an associative array. Considering that, and as you already have the variables that contain the id and quantity, the solution should be pretty obvious... Edited November 11 by maxxd typo Quote Share this post Link to post Share on other sites
Barand 1,397 Posted November 11 Here's an example if ($_SERVER['REQUEST_METHOD']=='POST') { $post = array_map('trim', $_POST); if (!empty($post['productid']) && !empty($post['qty'])) { if (!isset($_SESSION['cart'][$_POST['productid']])) { $_SESSION['cart'][$_POST['productid']] = 0; } $_SESSION['cart'][$_POST['productid']] += $_POST['qty']; } } from an earlier topic of yours ! Quote Share this post Link to post Share on other sites
makamo66 0 Posted November 11 22 hours ago, maxxd said: You can use variables as the index when you create an associative array. Considering that, and as you already have the variables that contain the id and quantity, the solution should be pretty obvious... When I use this: $cart_items[$id] = $quantity; or this: $cart_items = array($id => $quantity); I get the error: Illegal offset type Quote Share this post Link to post Share on other sites
maxxd 72 Posted November 11 $cart_items[$id] = $quantity; and $cart_items = array($id => $quantity); are two very different things. What's the value of $id? Quote Share this post Link to post Share on other sites
makamo66 0 Posted November 11 $id is defined like this: for($i=0; $i<=5; $i++){ if (isset($_REQUEST["element_id_$i"]) ) { $_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"]; $id = $_SESSION["element_id_$i"]; array_push($_SESSION["element_id"],$id); } $id = $_SESSION["element_id"]; } and $quantity is similar. Quote Share this post Link to post Share on other sites
maxxd 72 Posted November 12 // $id may or may not exist, doesn't matter for ($i = 0; $i <= 5; $i++) { if (isset($_REQUEST["element_id_$i"])) { // Now you're checking _any_ input for anything called element_id_{0-5} and assigning it to the session variable. // $id is still potentially undefined $_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"]; // OK, so now $id is a string or integer or float or whatever (validate your input, please) $id = $_SESSION["element_id_$i"]; // $_SESSION['element_id'] is an array because you're using array_push array_push($_SESSION["element_id"], $id); } // And now $id is an array because you're overwriting the value you've already put in there // Also, at this point if $_REQUEST['element_id_{0-5}'] _isn't_ set, $_SESSION['element_id'] should // not exist and you'll get an error $id = $_SESSION["element_id"]; } So $id is an array. You can use variables as array keys, but the value of those variables has to be a string or integer - you can't use an array as an index in another array. Quote Share this post Link to post Share on other sites