Jump to content

Recommended Posts

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>'; 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/205732-foreach-with-array-issue/
Share on other sites

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

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++; 
}

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

 

$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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.