Alko Alko - 9 months ago 73
PHP Question

Php implode empty array

I have a simple array with no values assigned:

[field1] =>
[field2] =>

then doing something like:

$result = array();
foreach ($array as $val) {
array_push($result, $val);

$data = implode("::", $result);

I end up with:


So how can I prevent implode generating separator if array values are empty?
If I have at least one value assigned:

[field1] => "hello"
[field2] =>

Then implode does it's job fine.


You can use array_filter(), for example in your case:


That will filter your array before imploding it.