如何在for或foreach中添加`if`?

时间:2015-08-09 13:32:32

标签: php

如果没有设置或找到,我试图在4 foreach中添加php if。这是一个例子

$images = "image1,image2,image3";
$covers = "cover1,cover2,cover3";
$titles = "title1,title2";
$albums = "album1,album2";

$images = explode(',', $images);
$covers = explode(',', $covers);
$titles = explode(',', $titles);
$albums = explode(',', $albums);

for ($index = 0; $index < count($images); $index++)
{
  echo "$images[$index] - $covers[$index] - $titles[$index] - $albums[$index]</br>";  
}

输出是:

    image1 - cover1 - title1 - album1
    image2 - cover2 - title2 - album2
    Notice: Undefined offset: 2 in C:\wamp\www\index.php on line 19
    Notice: Undefined offset: 2 in C:\wamp\www\index.php on line 19image3 - cover3 - - 

需要输出:

   image1 - cover1 - title1 - album1
   image2 - cover2 - title2 - album2
   image3 - cover3 -  - 
   image4 - cover4 -  - 

1 个答案:

答案 0 :(得分:2)

您需要查看isset()

你可以添加这样的东西

function run(){
  bar.width(0);
  bar.fadeIn(500,function(){
    bar.animate({'width': "100%"}, time).fadeOut(500,function(){
      change();run();
    });
  });
}