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.
Asked
Active
Viewed 1,597 times
0
-
update your question and show what you have and what you expected.. – jogesh_pi Sep 10 '12 at 04:47
-
http://stackoverflow.com/questions/2815162/is-there-a-php-function-like-pythons-zip – Rakesh Sep 10 '12 at 04:47
-
Show us your code, the output and the expected output. – Nir Alfasi Sep 10 '12 at 04:48
4 Answers
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