循环通过2个阵列并显示图像或不显示图像

时间:2014-03-03 10:56:25

标签: php arrays

我有2个阵列广告和adsimages,在用户执行搜索后从数据库中填充。一个阵列有广告,另一个有广告图片。一个广告可以有多个图片。

我想要的是以下内容:我想循环遍历第一个数组,如果第一个数组中列出的addid存在于第二个数组中,只显示第一个图像(可能有更多图像),如果没有,我想要显示noimage.jpg文件。然后我想转到第一个数组中的下一个项目,依此类推......

我正在打印数组的输出:

echo "<pre>";
print_r($adds);
echo "</pre>";
echo "<hr>";
echo "<pre>";
print_r($addsimages);
echo "</pre>";

die();

这产生以下输出:

        Array
    (
        [0] => Array
            (
                [addid] => 30
                [memberid] => 25
                [isnew] => 2
                [catid] => 1
            )

        [1] => Array
            (
                [addid] => 31
                [memberid] => 24
                [isnew] => 1
                [catid] => 1
            )

        [2] => Array
            (
                [addid] => 32
                [memberid] => 24
                [isnew] => 1
                [catid] => 1
            )

        [3] => Array
            (
                [addid] => 33
                [memberid] => 24
                [isnew] => 1
                [catid] => 1
            )

        [4] => Array
            (
                [addid] => 34
                [memberid] => 24
                [isnew] => 1
                [catid] => 1
            )

        [5] => Array
            (
                [addid] => 35
                [memberid] => 23
                [isnew] => 1
                [catid] => 1
            )

        [6] => Array
            (
                [addid] => 40
                [memberid] => 23
                [isnew] => 1
                [catid] => 1
            )

        [7] => Array
            (
                [addid] => 43
                [memberid] => 23
                [isnew] => 1
                [catid] => 1
            )

    )

        Array
    (
        [0] => Array
            (
                [imageid] => 122
                [addid] => 30
                [filename] => 2014-02-04-2017344667375_2_n.jpg
                [sorder] => 0
                [visible] => 1
            )

        [1] => Array
            (
                [imageid] => 123
                [addid] => 34
                [filename] => 2014-02-05-102031IMG_1025.jpg
                [sorder] => 0
                [visible] => 1
            )

        [2] => Array
            (
                [imageid] => 124
                [addid] => 34
                [filename] => 2014-02-05-102221IMG_1025.jpg
                [sorder] => 0
                [visible] => 1
            )

        [3] => Array
            (
                [imageid] => 136
                [addid] => 36
                [filename] => 2014-02-05-1039404667375_5_n.jpg
                [sorder] => 0
                [visible] => 1
            )

        [4] => Array
            (
                [imageid] => 151
                [addid] => 36
                [filename] => 2014-02-05-1048444667375_a_n.jpg
                [sorder] => 0
                [visible] => 1
            )

        [5] => Array
            (
                [imageid] => 152
                [addid] => 36
                [filename] => 2014-02-05-1048524667375_a_n.jpg
                [sorder] => 0
                [visible] => 1
            )

        [6] => Array
            (
                [imageid] => 153
                [addid] => 36
                [filename] => 2014-02-05-1050034667375_a_n.jpg
                [sorder] => 0
                [visible] => 1
            )

        [7] => Array
            (
                [imageid] => 155
                [addid] => 40
                [filename] => 2014-02-14-1813184515310_0_n.jpg
                [sorder] => 0
                [visible] => 1
            )

        [8] => Array
            (
                [imageid] => 154
                [addid] => 40
                [filename] => 2014-02-14-1807104515310_0_n.jpg
                [sorder] => 0
                [visible] => 1
            )

        [9] => Array
            (
                [imageid] => 162
                [addid] => 41
                [filename] => 2014-02-16-144406snowshill_lavender_harvesting_600x.jpg
                [sorder] => 0
                [visible] => 1
            )

        [10] => Array
            (
                [imageid] => 163
                [addid] => 41
                [filename] => 2014-02-16-1444404.jpg
                [sorder] => 0
                [visible] => 1
            )

        [11] => Array
            (
                [imageid] => 164
                [addid] => 43
                [filename] => 2014-02-22-1514464496160_0_n.jpg
                [sorder] => 0
                [visible] => 1
            )

        [12] => Array
            (
                [imageid] => 166
                [addid] => 43
                [filename] => 2014-02-22-1515324496160_3_n.jpg
                [sorder] => 0
                [visible] => 1
            )

        [13] => Array
            (
                [imageid] => 167
                [addid] => 43
                [filename] => 2014-02-22-1515554496160_3_n.jpg
                [sorder] => 0
                [visible] => 1
            )

    )

我试图用很多循环来做这件事,最后我什么也没出来。我的严格代码如下所示:

foreach($adds as $a) 
{
    foreach ($addsimages as $img) {
      if ($a['addid'] == $img['addid']) {
          echo $a['addid'] ."<br />";
          if($img['filename']) {
              echo $img['filename']."<br />";
              break;
          } 

      }  
   }



}

 foreach ($addsimages as $img) {
      if ($a['addid'] != $img['addid']) {
          echo $a['addid'] ."<br />";
              echo "no image <br />";
              break;
      }        
   }

其中输出以下内容:

2014-02-04-2017344667375_2_n.jpg
34
2014-02-05-102031IMG_1025.jpg
40
2014-02-14-1813184515310_0_n.jpg
43
2014-02-22-1514464496160_0_n.jpg
43
no image 

这显然不是我需要的。

3 个答案:

答案 0 :(得分:1)

我认为你的错误就在这里:

if($img['filename']) {

你想要检查什么?空()? isset()函数?现在它正在检查$ img ['filename']是真还是假。

答案 1 :(得分:1)

将两个条件放在同一个foreach循环中

foreach($adds as $a) 
{          
      $addImgIdKey = searchForId($a['addid'],$addsImages);  
      if (!is_null($addImgIdKey)) {

          echo $a['addid'] ."<br />";
          if(  isset($addsImages[$addImgIdKey]['filename']) 
              && !empty($addsImages[$addImgIdKey]['filename'])) {

              echo $addsImages[$addImgIdKey]['filename']."<br />";
          }                 
      }          
      else {
           echo $a['addid'] ."<br />";
           echo "no image <br />";                  

      }     
}

// get the id of the first match
function searchForId($id, $array) {
   foreach ($array as $key => $val) {
     if ($val['addid'] === $id) {
          return $key;
     }
   }
   return null;
}  

答案 2 :(得分:0)

如果你的adsimages数组是这样构建的话,如果可能的话会更容易(在我看来更漂亮):

Array (
    [<addid_1>] => Array
        (
           [<imageid_1>] => Array
               (
                  [filename] => filename_1.jpg
                  [sorder] => 0
                  [visible] => 1
               )

           [<imageid_1>] => Array
               (
                  [filename] => another_filename.jpg
                  [sorder] => 0
                  [visible] => 1
               )
        )

    [<addid_2>] => Array
        (
           [<imageid_1>] => Array
               (
                  [filename] => one_more.jpg
                  [sorder] => 0
                  [visible] => 1
               )
        )
)

使用&lt; addid_N&gt;和&lt; imageid_M&gt;取而代之的是他们的实际身份。

检索第一张图片的代码很简单:

foreach($ads as $ad) {
    if (isset($adsimages[ $ad['addid'] ][0]))
        echo '<img src="'.$adsimages[ $ad['addid'] ][0]['filename'].'"/>';
    else
        echo '<img src="noimage.jpg"/>';
}