0

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'] = " ";}
6
  • do you have more items in $portfolioItems [$i] not listed here? or are you wanting to do this for the whole array? Commented Feb 11, 2011 at 15:49
  • why don't you add values('mediaType','videoName') to an array and loop the if statement using the for loop? Commented Feb 11, 2011 at 15:50
  • are there more options in the portfolioItem[$i]['more options here']? or are you checking all the options individually? Commented Feb 11, 2011 at 15:51
  • 1
    I feel sorry for your fingers that you ever typed something like this out! Commented Feb 11, 2011 at 15:53
  • @Dan Grossman - isn't that what interns are for? :) Commented Feb 11, 2011 at 15:55

5 Answers 5

8
foreach ($portfolioItems[$i] as $key => $val) {
  if ($val == "") {
    $portfolioItems[$i][$key] = " ";
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks - this does the trick. Good to chop my bloated code down !
5

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);

2 Comments

Just for safety, unset($value); after the loop
@Mark Baker - file that one under the "duh" category. Thanks.
1

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

2 Comments

Don't you need the & to perform the modification? php.net/manual/en/control-structures.foreach.php
@Dan Grossman - Ah, yes, I see. Caffeine levels are hovering around empty at the moment. I glossed right over that little tidbit in this code. Thanks.
1

how about foreach-looping through your array?

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.