I have a bunch of variables I want to display of which some are empty. Some vars have spaces in them I want to preserve. I would like to display them as a comma delimited list. If I echo them sequentially var1, var2,.var6,.var10, I get extra commas where there are empties. Doesn't sound like it would be that hard to delete the extra commas, but my ideas have not worked.
Since I have many, many of these, I don't want to have to condition printing every one--allowing for first or last in placement of commas or iteratively replacing multiple commas with 1 comma or something complicated.. ie I'd like to find a simple repeatabl approach that can be used whenver this comes up.
One idea was to convert the string into an array and delete empty values. I can strip out empty spaces and echoing,can print var1,var2,,,var8,,, with no problem. However, I can't find a way to delete the commas ie, the empty values in array.
I tried:
$array = "one,two,,,six,,,ten";
$array= array_filter($array);
foreach($array as $val) {
echo $val;}}
foreach($array as $val) {
if ($val!=""&$val!=NULL) {
echo $val;}}
}
it doesn't get rid of commas. Have not had luck with following suggestions on web:
array_flip(array_flip($array); or
$array = array_values($array); or
Could be typo on my end, but would appreciate any suggestions from the experienced.
Thanks