snowman2344 Posted April 28, 2010 Share Posted April 28, 2010 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/199993-replace-%C2%A0-into-foreach/ Share on other sites More sharing options...
Alex Posted April 28, 2010 Share Posted April 28, 2010 Try: list($date, $h_a, $w_l_t, $us, $them, $vs_team) = array_map( create_function( '$x', 'return empty($x) ? " " : $x;' ), explode(",", $game_result) ); Quote Link to comment https://forums.phpfreaks.com/topic/199993-replace-%C2%A0-into-foreach/#findComment-1049683 Share on other sites More sharing options...
snowman2344 Posted April 28, 2010 Author Share Posted April 28, 2010 Works good except when the value is 0. it replaces the 0 with . Any way around this Thanks for the quick responce Quote Link to comment https://forums.phpfreaks.com/topic/199993-replace-%C2%A0-into-foreach/#findComment-1049689 Share on other sites More sharing options...
Alex Posted April 28, 2010 Share Posted April 28, 2010 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) ); Quote Link to comment https://forums.phpfreaks.com/topic/199993-replace-%C2%A0-into-foreach/#findComment-1049691 Share on other sites More sharing options...
snowman2344 Posted April 28, 2010 Author Share Posted April 28, 2010 Excelent thanks Quote Link to comment https://forums.phpfreaks.com/topic/199993-replace-%C2%A0-into-foreach/#findComment-1049694 Share on other sites More sharing options...
thewooleymammoth Posted April 28, 2010 Share Posted April 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/199993-replace-%C2%A0-into-foreach/#findComment-1049696 Share on other sites More sharing options...
Alex Posted April 28, 2010 Share Posted April 28, 2010 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) ); Quote Link to comment https://forums.phpfreaks.com/topic/199993-replace-%C2%A0-into-foreach/#findComment-1049698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.