对于影响内爆输出的空变量,有没有一个优雅的解决方案


Is there an elegant solution to empty variables affecting implode output?

这是我的代码:

$a = $request->getParameter('a');
$b = $request->getParameter('b');
$c = $request->getParameter('c');
$d = array($a, $b, $c);
$e = implode(", ", $d);

不幸的是,如果a、b或c为空(这些都来自HTML复选框),则表示其格式不正确。例如,如果b未被选中,它将看起来像这样:

a, , c

我希望它看起来像这样:

a, c

我知道我可以检查每个是否都是空的,如果是,从数组行中删除那个,但看起来会很乱。有没有更优雅的方法来修复它?

$e = implode(", ", array_filter($d));