samtwilliams Posted June 24, 2010 Share Posted June 24, 2010 For some reason it will only show one result. when ther are multiple in my array. My array data Array ( [0] => Array ( [CD0002] => 7 ) [1] => Array ( [CD0004] => 5 ) [2] => Array ( [CD0006] => 12 ) ) My array <?PHP $itemarrayview = $_SESSION['ItemTotalArray']; foreach($itemarrayview[0] as $key => $value) { echo '<tr>'; echo '<td>'.$key."</td>"; echo '<td>'.$value."</td>"; echo '<td align="center"><a href="pt_allocate.php?remove='.$key.'&asi='.$ASI_index.'">Create</a></td>'; echo '</tr>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/ Share on other sites More sharing options...
ToonMariner Posted June 24, 2010 Share Posted June 24, 2010 that would be because $itemarrayview[0] is only one record... Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076545 Share on other sites More sharing options...
samtwilliams Posted June 24, 2010 Author Share Posted June 24, 2010 I see that now, but struggling to fix it, if i place it within another foreach loop it works but then all my data elements become offset and i cant seem to fix them Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076551 Share on other sites More sharing options...
samtwilliams Posted June 24, 2010 Author Share Posted June 24, 2010 I now have this; <?PHP $itemarrayview = $_SESSION['ItemTotalArray']; unset($i); $i = 0; foreach ($itemarrayview as $key => $value) { foreach($itemarrayview[$i] as $key => $value) { echo '<tr>'; echo '<td>'.$key."</td>"; echo '<td>'.$value."</td>"; echo '<td align="center"><a href="pt_allocate.php?remove=1&asi='.$ASI_index.'&offset='.$i.'">Remove</a></td>'; echo '</tr>'; } $i++; } ?> which echo's my multidimensional array in a table, the only thing is if i remove one of them when i remove another i get offset errors everywhere. Anyone kind enough to help? Thanks Sam Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076599 Share on other sites More sharing options...
kenrbnsn Posted June 24, 2010 Share Posted June 24, 2010 Don't use "$key" & "$value" for both foreach loops. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076601 Share on other sites More sharing options...
samtwilliams Posted June 24, 2010 Author Share Posted June 24, 2010 Thanks Ken, I have changed that which has relieved the main issue, except how do i return the array key value, as I need it to remove elements using my remove link, as you can see i use $i in the link but thats not correct if there is 1 element in the array. $itemarrayview = $_SESSION['ItemTotalArray']; unset($i); $i = 0; foreach ($itemarrayview as $value) { foreach($value as $key => $value) { echo '<tr>'; echo '<td>'.$key."</td>"; echo '<td>'.$value."</td>"; echo '<td align="center"><a href="pt_allocate.php?remove=1&asi='.$ASI_index.'&offset='.$value.'">Remove</a></td>'; echo '</tr>'; } $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076614 Share on other sites More sharing options...
kenrbnsn Posted June 24, 2010 Share Posted June 24, 2010 You're still using the variable $value twice: <?php foreach ($itemarrayview as $value) { foreach($value as $key => $value) { ?> Change it to <?php foreach ($itemarrayview as $v) { foreach($v as $key => $value) { ?> And then decide which $value you want to use in your code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076617 Share on other sites More sharing options...
samtwilliams Posted June 24, 2010 Author Share Posted June 24, 2010 Have just changed that but still cant work out how to return the key from the first array to go in my removal link. Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076620 Share on other sites More sharing options...
kenrbnsn Posted June 24, 2010 Share Posted June 24, 2010 Please post you're current script. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076623 Share on other sites More sharing options...
samtwilliams Posted June 24, 2010 Author Share Posted June 24, 2010 $itemarrayview = $_SESSION['ItemTotalArray']; unset($i); $i = 0; foreach ($itemarrayview as $v) { foreach($v as $key => $value) { echo '<tr>'; echo '<td>'.$key."</td>"; echo '<td>'.$value."</td>"; echo '<td align="center"><a href="pt_allocate.php?remove=1&asi='.$ASI_index.'&offset='.$i.'">Remove</a></td>'; echo '</tr>'; } $i++; } An example array data; Array ( [1] => Array ( [CD0005] => 2 ) ) I want to put the first array Key ('1' in this example) into my remove link so i can use this code; unset($_SESSION['ItemTotalArray'][$offset]); Thanks Ken Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076626 Share on other sites More sharing options...
kenrbnsn Posted June 24, 2010 Share Posted June 24, 2010 It would help if you indented you code better. Change your first "foreach" and the echo line as seen below: <?php $itemarrayview = $_SESSION['ItemTotalArray']; unset($i); $i = 0; foreach ($itemarrayview as $k => $v) { foreach($v as $key => $value) { echo '<tr>'; echo '<td>'.$key."</td>"; echo '<td>'.$value."</td>"; echo '<td align="center"><a href="pt_allocate.php?remove=1&asi='.$ASI_index.'&offset='.$k.'">Remove</a></td>'; echo '</tr>'; } $i++; } ?> BTW, this doesn't make any sense: <?php unset($i); $i = 0; ?> Just set $i to 0, without using the unset. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/#findComment-1076634 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.