dongzhiyan5693
2012-09-10 04:44
浏览 82
已采纳

如何在一个foreach循环中显示两个数组?

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.

图片转代码服务由CSDN问答提供 功能建议

我在一列中存储两个数组。第一个是存储为image1 * image2 的图像 * ...等,第二个描述为description1 * description2 * ...等。 我想在一个foreach循环中使用这两组数组。请帮助。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dstbp22002 2012-09-10 04:48
    已采纳

    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];
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douchensou6495 2012-09-10 04:48

    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];
    }
    
    评论
    解决 无用
    打赏 举报
  • doufeng5059 2012-09-10 04:50

    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;
    }
    
    评论
    解决 无用
    打赏 举报
  • doutaoer3148 2012-09-10 04:51

    Just reference the key:

    foreach ($images as $key => $val) {
        echo '<img src="' . $val . '" alt="' . $descriptions[$key] . '" /><br />';
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题