Jump to content

foreach with session variables containing an array


Phaelon

Recommended Posts

Hi all,

I have multiple session varriables:

$_SESSION['cart']['content']['product1']
$_SESSION['cart']['content']['product2']
$_SESSION['cart']['content']['product3']

These have array data in them, such as (productnumber = 1, size = 3)

How can I do a foreach for each of these variables and then using the array data within? Do I do?:

foreach ($_SESSION['cart']['content'] as $content) {
    echo $content['productnumber'];
    echo $content['size'];
}

You might try to get the key for each one? So

foreach ($_SESSION['cart']['content'] as $key => $content) {
    echo $content['productnumber'];
    echo $content['size'];
}

You may also want to look at http://www.php.net/manual/en/function.array-keys.php This function might be able to help. If you get a list of the array keys of the very last items (the products) then you could do a foreach on that array.

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.