嵌套的IF语句PHP

时间:2011-02-11 15:46:27

标签: php

经过一段时间的努力之后 - 我有一个相当冗长的嵌套if语句,我正在研究如何改进它并使其更有效率。我确实想到了一个开关 - 但我看不出我怎么能分开它。

赞赏任何想法

  $portfolioItems [$i] = array(//assign vars into array
  'imagePath' => '/sites/jda_redev/'.$myrow_home->path.'/thumbs/thumbs_'.$myrow_home->filename,
  'altText' => $myrow_home->alttext,
  'description' => $myrow_home->description,
  'client' => $extra_client->field_value,
  'job' => $extra_job->field_value,
  'channel' => $extra_channel->field_value,
  'channeltwo' => $extra_channeltwo->field_value,
  'channelthree' => $extra_channelthree->field_value,
  'channelfour' => $extra_channelfour->field_value,
  'channelfive' => $extra_channelfive->field_value,
  'sector' => $extra_sector->field_value,
  'workerone' => $extra_workerone->field_value,
  'workertwo' => $extra_workertwo->field_value,
  'gallery' => $galleryName,
  'mediaType' => $media_type->field_value,
  'videoName' => $video_name->field_value
  );

if ( $portfolioItems [$i]['imagePath'] == "" )
      {$portfolioItems [$i]['imagePath'] = " ";}
      if ( $portfolioItems [$i]['altText'] == "" )
      {$portfolioItems [$i]['altText'] = " ";}
      if ( $portfolioItems [$i]['description'] == "" )
      {$portfolioItems [$i]['description'] = " ";}
      if ( $portfolioItems [$i]['client'] == "" )
      {$portfolioItems [$i]['client'] = " ";}
      if ( $portfolioItems [$i]['job'] == "" )
      {$portfolioItems [$i]['job'] = " ";}
      if ( $portfolioItems [$i]['channel'] == "" )
      {$portfolioItems [$i]['channel'] = " ";}
      if ( $portfolioItems [$i]['channeltwo'] == "" )
      {$portfolioItems [$i]['channeltwo'] = " ";}
      if ( $portfolioItems [$i]['channelthree'] == "" )
      {$portfolioItems [$i]['channelthree'] = " ";}
      if ( $portfolioItems [$i]['channelfour'] == "" )
      {$portfolioItems [$i]['channelfour'] = " ";}
      if ( $portfolioItems [$i]['channelfive'] == "" )
      {$portfolioItems [$i]['channelfive'] = " ";}
      if ( $portfolioItems [$i]['sector'] == "" )
      {$portfolioItems [$i]['sector'] = " ";}
      if ( $portfolioItems [$i]['workerone'] == "" )
      {$portfolioItems [$i]['workerone'] = " ";}
      if ( $portfolioItems [$i]['workertwo'] == "" )
      {$portfolioItems [$i]['workertwo'] = " ";}
      if ( $portfolioItems [$i]['gallery'] == "" )
      {$portfolioItems [$i]['gallery'] = " ";}
      if ( $portfolioItems [$i]['mediaType'] == "" )
      {$portfolioItems [$i]['mediaType'] = " ";}
      if ( $portfolioItems [$i]['videoName'] == "" )
      {$portfolioItems [$i]['videoName'] = " ";}

5 个答案:

答案 0 :(得分:8)

foreach ($portfolioItems[$i] as $key => $val) {
  if ($val == "") {
    $portfolioItems[$i][$key] = " ";
  }
}

答案 1 :(得分:5)

您可以使用foreach次迭代来完成它:

foreach ($portfolioItems[$i] as &$value) {
    if($value == "") $value = " ";
}

// Thanks @Mark Baker, I forgot this very important line:
unset($value);

答案 2 :(得分:1)

这个怎么样?

$portfolioKeys = Array('sector', 'workerone'); // Add all keys to check in here

foreach($portfolioKeys as $key) {
    if ($portfolioItems[$i][$key]) == "") {
       $portfolioItems[$i][$key] = "";
    }
}

答案 3 :(得分:1)

答案 4 :(得分:0)

我没有看到嵌套条件,但无论如何,看起来你可以(也许 - 如果这些都是你的元素):

foreach ($portfolioItems[$i] as $k => $v) {
  $portfolioItems[$i][$k] = ($v == "")? " " : $v;
}