时间:2021-07-01 10:21:17 帮助过:7人阅读
简便的办法不知道
用if有什么不好吗?
$buf = array($a, $b, $c, $d);echo join(',', array_filter($buf, 'cmp'));function cmp($m) { return ! empty($m);}
$s = "$a,$b,$c,$d";$ar = preg_split('/,/', $s, -1, PREG_SPLIT_NO_EMPTY);echo implode(',', $ar);
$a = 'a';$b = '';$c = '';$d = 'd';echo handle($a,$b,$c,$d);function handle($a,$b,$c,$d){ $arr = array(); array_push($arr, $a,$b,$c,$d); $ret = trim(implode(',', $arr),','); $ret = preg_replace('/[,]{2,}/', ',', $ret); return $ret;}
修改了一下,刚才那个没有考虑变量中含有","的情况。
$a = 'a';$b = 'b';$c = 'c';$d = 'd';$arr = array($a,$b,$c,$d);$ret = array();foreach($arr as $val){ if($val!=''){ array_push($ret, $val); }}echo implode(',', $ret);