xander85 Posted September 2, 2007 Share Posted September 2, 2007 Hi All, I did a search for other foreach() problems and cant seem to find a solution that fits my syntax, probably because I'm a newbie. Here is my code: if($submit) { $_SESSION['firstname'] = $_POST['firstname']; $_SESSION['lastname'] = $_POST['lastname']; $_SESSION['id'] = $_POST['id']; if(!isset($_SESSION['box'])) { $CartItem = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']); $_SESSION['box'] = $CartItem; } else { $CartItem = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']); array_push($_SESSION['box'], $CartItem); } echo sizeof($_SESSION['box']); ?> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <? foreach($_SESSION['box'] as $CartItem[]=>$items) { echo "<tr>"; foreach($items as $item) *****Line 32******* { echo "<td>$item</td>"; } echo "</tr>"; } ?> </table> I get this error: Warning: Invalid argument supplied for foreach() in /home/content/n/g/c/ngcc1/html/yui/yui/examples/container/test.php on line 32 It is displayed three times; however, my current array is 27 elements. Quote Link to comment https://forums.phpfreaks.com/topic/67603-solved-help-with-foreach-multidimensional-array/ Share on other sites More sharing options...
sasa Posted September 2, 2007 Share Posted September 2, 2007 change if(!isset($_SESSION['box'])) { $CartItem = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']); $_SESSION['box'] = $CartItem; } else { $CartItem = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']); array_push($_SESSION['box'], $CartItem); } to $_SESSION['box'][] = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']); Quote Link to comment https://forums.phpfreaks.com/topic/67603-solved-help-with-foreach-multidimensional-array/#findComment-339629 Share on other sites More sharing options...
xander85 Posted September 2, 2007 Author Share Posted September 2, 2007 That doesnt do anything, I still get warning for the same line. Quote Link to comment https://forums.phpfreaks.com/topic/67603-solved-help-with-foreach-multidimensional-array/#findComment-339872 Share on other sites More sharing options...
wildteen88 Posted September 2, 2007 Share Posted September 2, 2007 When you setup the $_SESSION['box'] array, you are not setting up a multi dimension array. Instead you are setting up a 2d array. Only until the else block gets executed $_SESSION['box'] becomes a multidimensional array. Eg: Whn you first run that code and $_SESSION['box'] is not set. It'll create following array: Array ( [0] => username_here [1] => password_here [2] => id_here ) When that block of code is executed again, the else part will execute as $_SESSION['box'] now exists. However when it is executed the array will turn into this: Array ( [0] => username_here [1] => password_here [2] => id_here [3] => Array ( [0] => username_here [1] => password_here [2] => id_here ) ) $_SESSION['box'] becomes an multidimensional array at index 3. This is the problem. To fix it you'll need to do this: Change this line: $_SESSION['box'] = $CartItem; To: $_SESSION['box'][] = $CartItem; However you will need to unset the first 3 keys in order for your code to work. Or you could just fix the first 3 keys for the box session. with this: if(!is_array($_SESSION['box'][0])) { $_SESSION['box'][0] = array($_SESSION['box'][0], $_SESSION['box'][1], $_SESSION['box'][2]); unset($_SESSION['box'][1], $_SESSION['box'][2]); } You can remove the above three lines once you have run the code. This will just fix the session format. Quote Link to comment https://forums.phpfreaks.com/topic/67603-solved-help-with-foreach-multidimensional-array/#findComment-339892 Share on other sites More sharing options...
xander85 Posted September 2, 2007 Author Share Posted September 2, 2007 I figured it out thanks! It actually turns out the warnings were from the first three entries into the session array that were not inserted corrected and had not been unset or the session was destroyed. I destroyed the session and it worked out fine. Quote Link to comment https://forums.phpfreaks.com/topic/67603-solved-help-with-foreach-multidimensional-array/#findComment-339899 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.