PHP: Remove empty values from array, then convert that to a string -


i know how each of these things separately (remove empty values, , convert array comma-separated string) can't them work in combination, , haven't yet been able find way so. know can use print_r display results of filter, that's not helpful because need send resulting string database (that's day). appreciated!

i have:

$array = array('item1', 'item2', '', 'item4'); //this should filter out empty values (index 3) $filter = array(array_filter($array)); //this should take filtered array , convert comma-separated string $comma_separated = implode(",", $filter); echo $comma_separated; 

every time try output just:

array 

try way,no need push after filtering array & $orderarray?

$array = array('item1', 'item2', '', 'item4'); $filter=array_filter($array); // see here, didn't add array() $comma_separated = implode(",", $filter); echo $comma_separated; 

edit: shorter way it, courtesy @mhakvoort

  $comma_separated = implode(",", array_filter($array)); 

array_filter: "if no callback supplied, entries of input equal false removed." means elements values null, 0, '0', '', false, array() removed it.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -