makamo66 Posted November 11, 2019 Share Posted November 11, 2019 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 Link to comment https://forums.phpfreaks.com/topic/309509-how-to-make-an-associative-array-with-key-value-pairs/ Share on other sites More sharing options...
maxxd Posted November 11, 2019 Share Posted November 11, 2019 (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, 2019 by maxxd typo Quote Link to comment https://forums.phpfreaks.com/topic/309509-how-to-make-an-associative-array-with-key-value-pairs/#findComment-1571456 Share on other sites More sharing options...
Barand Posted November 11, 2019 Share Posted November 11, 2019 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 Link to comment https://forums.phpfreaks.com/topic/309509-how-to-make-an-associative-array-with-key-value-pairs/#findComment-1571461 Share on other sites More sharing options...
makamo66 Posted November 11, 2019 Author Share Posted November 11, 2019 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 Link to comment https://forums.phpfreaks.com/topic/309509-how-to-make-an-associative-array-with-key-value-pairs/#findComment-1571500 Share on other sites More sharing options...
maxxd Posted November 11, 2019 Share Posted November 11, 2019 $cart_items[$id] = $quantity; and $cart_items = array($id => $quantity); are two very different things. What's the value of $id? Quote Link to comment https://forums.phpfreaks.com/topic/309509-how-to-make-an-associative-array-with-key-value-pairs/#findComment-1571501 Share on other sites More sharing options...
makamo66 Posted November 11, 2019 Author Share Posted November 11, 2019 $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 Link to comment https://forums.phpfreaks.com/topic/309509-how-to-make-an-associative-array-with-key-value-pairs/#findComment-1571502 Share on other sites More sharing options...
maxxd Posted November 12, 2019 Share Posted November 12, 2019 // $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 Link to comment https://forums.phpfreaks.com/topic/309509-how-to-make-an-associative-array-with-key-value-pairs/#findComment-1571506 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.