Jump to content

Finding out child variable name


Phaelon

Recommended Posts

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']

Link to comment
https://forums.phpfreaks.com/topic/288275-finding-out-child-variable-name/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.