0

am storing two arrays in one column.first one is images stored as image1*image2*...etc and second one is descriptions as description1*description2*...etc. i want to use these two set of arrays in one foreach loop.Please help.

user1586851
  • 1
  • 1
  • 3

4 Answers4

2

Just reference the key:

foreach ($images as $key => $val) {
    echo '<img src="' . $val . '" alt="' . $descriptions[$key] . '" /><br />';
}
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
1

You can't use foreach, but you can use for and indexed access like so.

$count = count($images);
for ($i = 0; $i < $count; $i++) {
    $image = $images[$i];
    $description = $descriptions[$i];
}
ricick
  • 5,694
  • 3
  • 25
  • 37
1

You could use array_combine to combine the two arrays and then use a foreach loop.

$images = array('image1', 'image2', ...);
$descriptions = array('description1', 'description2', ...);

foreach (array_combine($images, $descriptions) as $image => $desc) {
  echo $image, $desc;
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

It does not seem possible by foreach loop. Instead try using for loop. If you are sure both your arrays are of the same size, then try using following code:

for ($i=0; $i<sizeof(array1); $i++) {
     echo $arrray1[$i];
     echo $arrray2[$i];
}
harshit
  • 3,788
  • 3
  • 31
  • 54