Jump to content

replace   into foreach


snowman2344

Recommended Posts

I have the following code that works great except when the variable is blank. I need to replace any blank varibles with  

 

Is there a Guru that can help

 

 
$game_results = file("include/game_results.csv");
foreach ($game_results as $game_result) {
list($date, $h_a, $w_l_t, $us, $them, $vs_team) = explode(",", $game_result);
echo "<tr><td align='center'>$date</td>
                 <td align='center'>$h_a</td>
                 <td align='center'>$w_l_t</td>
 <td align='center'>$us</td>
 <td align='center'>$them</td>		  
  <td align='center'>$vs_team</td>  </tr>";
}

Link to comment
Share on other sites

empty returns true for 0, so compare it against null instead.

 

list($date, $h_a, $w_l_t, $us, $them, $vs_team) = 'hi,ho,hl,0,hm,hn';
$arr = array_map(
	create_function(
		'$x',
		'return $x == null ? " " : $x;'
	),
	explode(",", $game_result)
);

Link to comment
Share on other sites

i just read the manual on create_function. I think im missing the point, lemme ask if i got this right...

 

its a function where you input as a string what you want the function to do, with the variables that you also input?

 

if im right, i have to say: What the hell? why wouldnt you just write out what you want to do in plain code?

 

 

Link to comment
Share on other sites

Anonymous functions aren't available until PHP 5.3.0, if you were writing that code for a system with PHP 5.3.0 or higher installed you might do it this way:

 

list($date, $h_a, $w_l_t, $us, $them, $vs_team) = array_map(
	function($x){
		return $x == null ? " " : $x;
	},
	explode(",", $game_result)
);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.