gazever Posted February 22, 2007 Share Posted February 22, 2007 Hi I was given this code yesterday by effigy, <pre> <?php $data = '8,6,9,2,,,7,4,5,3,,,2,1,5,3,4,,,6,9,4,5,3,6,9,8'; $data = str_replace(',,', ', ,', $data); $array = explode(',', $data); print_r($array); ?> </pre> However this string opens from a txt file and replaces the ,, with a space character, I loop through this a few times, incase there are two empty values in a row, producing ,,, However how would I loop thorugh the array and replace all values that are "" empty to the value, as if the first value of the array is empty It will not change to a space? I'm sure this is really simple to do. Thanks Gaz Link to comment https://forums.phpfreaks.com/topic/39620-solved-arrays/ Share on other sites More sharing options...
monk.e.boy Posted February 22, 2007 Share Posted February 22, 2007 if( $data{0} == ',' ) { $data = '$nbsp;'.$data; print $data; } Try this. Check character 0, if it is a ',', insert a monk.e.boy Link to comment https://forums.phpfreaks.com/topic/39620-solved-arrays/#findComment-191202 Share on other sites More sharing options...
JasonLewis Posted February 22, 2007 Share Posted February 22, 2007 not $nbsp; monk.e.boy. Link to comment https://forums.phpfreaks.com/topic/39620-solved-arrays/#findComment-191204 Share on other sites More sharing options...
monk.e.boy Posted February 22, 2007 Share Posted February 22, 2007 not $nbsp; monk.e.boy. Oh yeah. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/39620-solved-arrays/#findComment-191211 Share on other sites More sharing options...
gazever Posted February 22, 2007 Author Share Posted February 22, 2007 thanks, sent me in the right direction, I used this within my existing loop if ($array[$i] == "") { $array[$i] = " "; } Gaz Link to comment https://forums.phpfreaks.com/topic/39620-solved-arrays/#findComment-191219 Share on other sites More sharing options...
monk.e.boy Posted February 23, 2007 Share Posted February 23, 2007 thanks, sent me in the right direction, I used this within my existing loop if ($array[$i] == "") { $array[$i] = " "; } Gaz if ( strlen( $array[$i] ) == 0) { $array[$i] = " "; } would be my prefered solution ;D ;D ;D (joke) monk.e.boy Link to comment https://forums.phpfreaks.com/topic/39620-solved-arrays/#findComment-192060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.