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']; } Link to comment https://forums.phpfreaks.com/topic/288245-foreach-with-session-variables-containing-an-array/ Share on other sites More sharing options...
DrMath Posted May 5, 2014 Share Posted May 5, 2014 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. Link to comment https://forums.phpfreaks.com/topic/288245-foreach-with-session-variables-containing-an-array/#findComment-1478214 Share on other sites More sharing options...
Phaelon Posted May 5, 2014 Author Share Posted May 5, 2014 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 Link to comment https://forums.phpfreaks.com/topic/288245-foreach-with-session-variables-containing-an-array/#findComment-1478215 Share on other sites More sharing options...
DrMath Posted May 5, 2014 Share Posted May 5, 2014 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. Link to comment https://forums.phpfreaks.com/topic/288245-foreach-with-session-variables-containing-an-array/#findComment-1478216 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. Link to comment https://forums.phpfreaks.com/topic/288245-foreach-with-session-variables-containing-an-array/#findComment-1478338 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.