开发者

Nested IF statement PHP

after a bit of adivce -I have a rather lengthy chunk of nested if statement, and I'm looking at ways I can refine it and make it more efficient. I did think of a switch - but I can't see how I could split this up.

Any thoughts appreciated

  $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'] = " ";}


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


You could use a foreach iteration to accomplish it:

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

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


How about this?

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

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


how about foreach-looping through your array?


I don't see a nested condition but anyways, it looks like you can (maybe - if those are ALL your elements) do:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜