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

Edited by Phaelon
Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.