thomashw Posted March 15, 2008 Share Posted March 15, 2008 I have a multidimensional array using sessions. The array consists last array in the multidimensional array has a 0 and 1 "key" but it's only displaying the value for the 0 key, and doesn't display the value corresponding to the 1 key. $j = 0; foreach($_SESSION['cart'][$pid]["$j"] as $key => $value) { echo "$value, "; $j++; } I'm also new to this, so this may be a stupid question. Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/ Share on other sites More sharing options...
sKunKbad Posted March 15, 2008 Share Posted March 15, 2008 This is fine for showing what is in the array, but you aren't showing what code made the array. This is where your mistake is. Show the code. Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492715 Share on other sites More sharing options...
thomashw Posted March 15, 2008 Author Share Posted March 15, 2008 $i = 0; while(isset($_POST["option$i"])) { // Add the options to the array $_SESSION['cart'][$pid][] = array("option$i" => $_POST["option$i"]); $i++; } Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492719 Share on other sites More sharing options...
sKunKbad Posted March 15, 2008 Share Posted March 15, 2008 one the final page with the results, use: <?php echo "<pre>"; print_r($_SESSION); echo "</pre>"; ?> and share that output with me. Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492722 Share on other sites More sharing options...
thomashw Posted March 15, 2008 Author Share Posted March 15, 2008 Array ( [cart] => Array ( [2] => Array ( [productid] => 2 [quantity] => 1 [price] => 299.99 [0] => Array ( [option0] => Bulb Type: H1 ) [1] => Array ( [option1] => Color Temperature: 4300K ) ) ) ) When I use the for loop, it only displays the Bulb Type: H1, and not the Color Temperature: 4300K. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492727 Share on other sites More sharing options...
sKunKbad Posted March 15, 2008 Share Posted March 15, 2008 If you are getting all this info from a database, wouldn't it be easier to just add a item # and quantity to the cart? When a user views the shopping cart or goes to check out, you could query your database for the details. A properly designed database of items would not have the same part number with options like: part 1, blue part 1, green part 1, pink instead you would have: part 1 part 2 part 3 Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492731 Share on other sites More sharing options...
thomashw Posted March 15, 2008 Author Share Posted March 15, 2008 The options are selected by the person on the site, and each part has multiple options to be selected, so I need to "remember" what they selected... there are also multiple categories for options, so if I entered all of the different ways the options could be selected, I'd have thousands for just one part. Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492733 Share on other sites More sharing options...
sKunKbad Posted March 15, 2008 Share Posted March 15, 2008 can you paste the formatted print_r. Until I fixed it, the output wasn't in pre tags. Take a look up there ^ Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492735 Share on other sites More sharing options...
thomashw Posted March 15, 2008 Author Share Posted March 15, 2008 Wow, that makes it so much more organized! Array ( [cart] => Array ( [2] => Array ( [productid] => 2 [quantity] => 1 [price] => 299.99 [0] => Array ( [option0] => Bulb Type: H1 ) [1] => Array ( [option1] => Color Temperature: 4300K ) ) ) ) Now that I see it like this, the options don't need to be in new arrays. It'd be great to have the options in the $pid array, but the $pid array is created higher in the code. How can I "add" the options to the end of the $pid array instead of creating a new array for the options? I realize I need to remove the "[]" from the end of this code, but how do I just "add" to the $pid array? $i = 0; while(isset($_POST["option$i"])) { // Add the options to the array $_SESSION['cart'][$pid][] = array("option$i" => $_POST["option$i"]); $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492742 Share on other sites More sharing options...
sKunKbad Posted March 15, 2008 Share Posted March 15, 2008 $_SESSION['cart'][2][] = 'some option'; will add 'some option' to the product in the array shown. I would create an options array inside the product array, that way you can loop through the options when outputting: $pid = 1; $_SESSION['cart']["$pid"]['options'][] = 'chrome'; $_SESSION['cart']["$pid"]['options'][] = 'gold plated'; $_SESSION['cart']["$pid"]['options'][] = 'diamond encrusted'; check the array with print_r(), and you will see that they have been added to the product options array Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492751 Share on other sites More sharing options...
thomashw Posted March 15, 2008 Author Share Posted March 15, 2008 You rock! Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/96253-multidimensional-array-help/#findComment-492759 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.