Phaelon Posted May 5, 2014 Share Posted May 5, 2014 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']; } Quote Link to comment Share on other sites More sharing options...
DrMath Posted May 5, 2014 Share Posted May 5, 2014 (edited) You need to go one step further I think. The way you have it now, the only thing $content can have is ['productx']. You need to go all the way to the product to get info about it. Edited May 5, 2014 by DrMath Quote Link to comment Share on other sites More sharing options...
Phaelon Posted May 5, 2014 Author Share Posted May 5, 2014 (edited) I can;t do that because the variable names are variable themself, so I can't hardcode their names in. So i am hoping there is a way for PHP to detect them and perform functions on their array Edited May 5, 2014 by Phaelon Quote Link to comment Share on other sites More sharing options...
Solution DrMath Posted May 5, 2014 Solution Share Posted May 5, 2014 (edited) 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. Edited May 5, 2014 by DrMath Quote Link to comment Share on other sites More sharing options...
Phaelon Posted May 6, 2014 Author Share Posted May 6, 2014 Thanks DrMath. Your method worked. Have a good day. 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.