makamo66 Posted November 2, 2019 Share Posted November 2, 2019 <?php $_SESSION["cart_item"] = array( 'cart_item' => array( 'id' => $id, 'product_name' => $product_name )); } $cart_items = $_SESSION["cart_item"]; foreach ($cart_items as $cart_item) { echo $cart_item["id"] . $cart_item["product_name"]; } ?> I have tried several variations of the foreach loop like the one above and I mostly get the error message: Notice: Array to string conversion. When I use: var_dump($cart_items); I get the following output: array(1) { ["cart_item"]=> array(2) { ["id"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "3" } ["product_name"]=> array(2) { [0]=> string(19) "Adult Female Bike" [1]=> string(18) "Kids Unisex Bike" } } } Quote Link to comment Share on other sites More sharing options...
requinix Posted November 2, 2019 Share Posted November 2, 2019 The item in $_SESSION is called "cart_item". One item. That's what it is, right? You then assign it to a variable named "cart_items". Plural. That does not magically make it have multiple items. You only have the one item. You can't foreach over an item. What you need is an array of items (see the plural?) which you can then foreach over. Quote Link to comment Share on other sites More sharing options...
makamo66 Posted November 2, 2019 Author Share Posted November 2, 2019 The inner arrays of the multidimensional $_SESSION arrays aren't single variables, they're $_SESSION arrays defined like below. for($i=1; $i<=6; $i++){ if (isset($_REQUEST["element_id_$i"]) && ($_REQUEST["element_id_$i"] != NULL) ) { $_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"]; $id = $_SESSION["element_id_$i"]; array_push($_SESSION["element_id"],$id); } $id = $_SESSION["element_id"]; if (isset($_REQUEST["product_$i"]) && ($_REQUEST["product_$i"] != NULL) ) { $_SESSION["product_name_$i"] = $_REQUEST["product_$i"]; $product_name = $_SESSION["product_name_$i"]; array_push($_SESSION["product_name"],$product_name); } $product_name = $_SESSION["product_name"]; } Quote Link to comment Share on other sites More sharing options...
requinix Posted November 2, 2019 Share Posted November 2, 2019 I'm not sure I understand. You posted some code earlier and asked why it didn't work, and are now saying that this other code is somehow responsible for it? Look at your code again, but this time with proper indentation: $_SESSION["cart_item"] = array( // <- this will be $cart_items 'cart_item' => array( // <- so this is what the foreach will find 'id' => $id, 'product_name' => $product_name ) ); Quote Link to comment Share on other sites More sharing options...
makamo66 Posted November 2, 2019 Author Share Posted November 2, 2019 I retract my original question because I don't need to make my multidimensional-array any larger after all. Please ignore my post. Quote Link to comment 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.