-1

I have three arrays as follows:

$names = array ("Josh", "Dami");
$details = array ("Douglas", "Port Erin");
$images = array ("1", "2");

I then have the following foreach loop to put each other the values into it's own text field

<?php foreach($names as $a_name) { ?>
  <tr>
  <td><input type="text" size="10"  name="names[]"   value="<?php  echo $a_name ?>" ></td>
  <td><input type="text" size="10"  name="details[]"   value="<?php  echo $a_detail ?>" ></td>
  <td><input type="text" size="10"  name="images[]"   value="<?php  echo $a_image ?>" ></td>
  <td><?php echo $add_row_icon; echo (' '); echo $remove_row_icon;?></td>
  </tr> <?php  
}?>

Obviously that only runs through the names array and puts those into the correct fields. How would I go about nesting two more foreach loops so that the names, details and images were shown?

JoshuaBrand
  • 175
  • 1
  • 14

4 Answers4

2

Try this :

<?php foreach($names as $key => $a_name) { ?>
    <tr><td><input type="text" size="10"  name="details[]"   value="<?php  echo $details[$key] ?>" ></td><tr>
<?php } ?>

code is only for details, use the same thing for others too.

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
1
$names = array ("Josh", "Dami");
$details = array ("Douglas", "Port Erin");
$images = array ("1", "2");

// get the smallest of the arrays
$size = min(count($names), count($details), count($images));

<?php for ($i=0; $i<$size; $i++) { ?>
<tr>
  <td><input type="text" size="10" name="names[]" value="<?php echo $names[$i]; ?>" ></td>
  <td><input type="text" size="10" name="details[]" value="<?php echo $details[$i]; ?>" ></td>
  <td><input type="text" size="10" name="images[]" value="<?php echo $images[$i]; ?>" ></td>
  <td><?php echo $add_row_icon; echo (' '); echo $remove_row_icon; ?></td>
</tr> 
<?php } ?>
Thomas
  • 364
  • 2
  • 9
  • -1 for using `for` loop and using the `sizeof` method even (which is not that common and is an alias of `count`). Though it may not differ from `foreach` for such small array the performance will differ for bigger arrays. – shadyyx Jul 25 '14 at 10:22
  • Replaced the sizeof() by count(), as this is the function we should be using. I think the for loop here is appropriate because we can easily loop over the three arrays in one go. Please note that I do create the $size first before entering the for loop. Running count on every iteration is the reason the for loop is in general more expensive than a foreach. That is not the case in this example. – Thomas Jul 25 '14 at 11:31
  • OK, -1 retracted. But `for` loop even with `count` outside is little bit more expensive than `foreach`... – shadyyx Jul 25 '14 at 13:52
0
$names = array ("Josh", "Dami");
$details = array ("Douglas", "Port Erin");
$images = array ("1", "2");

<?php for ($i=0; $i<sizeof($names); $i++) { ?>
  <tr>
  <td><input type="text" size="10"  name="names[]"   value="<?php  echo $names[$i] ?>" ></td>
  <td><input type="text" size="10"  name="details[]"   value="<?php  echo $details[$i] ?>" ></td>
  <td><input type="text" size="10"  name="images[]"   value="<?php  echo $images[$i] ?>" ></td>
  <td><?php echo $add_row_icon; echo (' '); echo $remove_row_icon;?></td>
  </tr> <?php  
}?>
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • -1 for using `for` loop and `sizeof` inside of `for` loop. Though it may not differ from `foreach` for such small array the performance will differ for bigger arrays. – shadyyx Jul 25 '14 at 10:20
-1

You should be using foreach loop as in Your question - you only forgot to loop with retrieving of the index key, thus:

<?php foreach ($names as $key => $a_name) { ?>
  <tr>
  <td><input type="text" size="10"  name="names[]"   value="<?php  echo $a_name ?>" ></td>
  <td><input type="text" size="10"  name="details[]"   value="<?php  echo $details[$key] ?>" ></td>
  <td><input type="text" size="10"  name="images[]"   value="<?php  echo $images[$key] ?>" ></td>
  <td><?php echo $add_row_icon; echo (' '); echo $remove_row_icon;?></td>
  </tr> <?php  
} ?>

But to be honest I'd think about changing those many arrays into just one multidimensional array looking like this:

$users = array(
    array(
        'name' => 'Josh',
        'detail' => 'Douglas',
        'image' => '1',
    ),
    array(
        'name' => 'Dami',
        'detail' => 'Port Erin',
        'image' => '2',
    ),
);

Also do not rely on names[] and details[] are being sent in the right order (it may happen that the values could be swapped) therefore use indexes where possible. Now what you need to do is:

<?php foreach ($users as $i => $user) { ?>
<tr>
    <td><input type="text" size="10" name="names[<?php echo $i ?>]" value="<?php echo $user['name'] ?>" ></td>
    <td><input type="text" size="10" name="details[<?php echo $i ?>]" value="<?php echo $user['detail'] ?>" ></td>
    <td><input type="text" size="10" name="images[<?php echo $i ?>]" value="<?php echo $user['image'] ?>" ></td>
    <td><?php echo "{$add_row_icon} {$remove_row_icon}" ?></td>
</tr>
<?php } ?>
shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • -1 for using foreach $key=>$value on arrays, that are not guaranteed to be key-synchronous – Eugen Rieck Jul 25 '14 at 11:42
  • @EugenRieck BTW, that's the reason as well why I suggested to create one multidimensional array instead of using three arrays so your downvote only proves my previous statement. – shadyyx Jul 25 '14 at 14:09