Phaelon Posted May 6, 2014 Share Posted May 6, 2014 (edited) Is it possible to find out the name of a child variable? I have: $_SESSION['cart']['content']['foo'] $_SESSION['cart']['content']['bar'] I want to basically do: foreach ($_SESSION['cart']['content'][*] as $content) { echo $content['productname']; } Notice the star I am using above to express what I am trying to mean. Ultimately PHP would echo: $_SESSION['cart']['content']['foo']['productname'] $_SESSION['cart']['content']['bar']['productname'] Edited May 6, 2014 by Phaelon Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 6, 2014 Share Posted May 6, 2014 I'm probably missing something, but the foreach should do what you want without the "[*]" part. foreach ($_SESSION['cart']['content'] as $content) { echo $content['productname']; } This should display the product name for both "foo" and "bar". Quote Link to comment Share on other sites More sharing options...
Phaelon Posted May 6, 2014 Author Share Posted May 6, 2014 my shopping cart is a total POS and I need to start from scratch and sleep. Thanks anyway. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 6, 2014 Share Posted May 6, 2014 you may want to consider that this is the 21st century, computer storage is large and cheap, and each size or color of an item can be given a different id (identifier.) your cart needs to store the item id and the quantity - $_SESSION['cart'][item_id] = quantity; $_SESSION['cart'][1] = 1; // item id 1, quantity 1 $_SESSION['cart'][45] = 2; // item id 45, quantity 2 you wold manipulate the quantity in the cart using the item id as the array key to test for and access the cart entry. to display the cart contents, you would retrieve all the item id's at once (see array_keys()), run one database query to get the information for the items in the cart, pre-process the retrieved information to store it in an array using the item id as the array key (has the same structure as the cart), then as you loop over the cart contents to display it, use the item id from the cart to access the retrieved information about each item. 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.