How can I implode an array while skipping empty array items?

前端 未结 9 534
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 12:45

Perl\'s join() ignores (skips) empty array values; PHP\'s implode() does not appear to.

Suppose I have an array:

$array = a         


        
9条回答
  •  悲&欢浪女
    2020-12-23 13:46

    $array = ["one", NULL, "two", NULL, "three"];
    $string = implode("-", array_diff($array, [NULL]));
    echo $string;
    

    Returns one-two-three

提交回复
热议问题