Alko Alko - 1 year ago 161
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.

Answer Source

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


That will filter your array before imploding it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download