Perl's join()
ignores (skips) empty array values; PHP's implode()
does not appear to.
Suppose I have an array:
$array = array('one', '', '', 'four', '', 'six');
implode('-', $array);
yields:
one---four--six
instead of (IMHO the preferable):
one-four-six
Any other built-ins with the behaviour I'm looking for? Or is it going to be a custom jobbie?
Try this:
To remove
null
,false
,empty
string but preserve0
, etc. use func. 'strlen
'will output:
I suppose you can't consider it built in (because the function is running with a user defined function), but you could always use array_filter.
Something like:
How you should implement you filter only depends on what you see as "empty".
Try this:
You can use
array_filter()
:Obviously this will not work if you have
0
(or any other value that evaluates tofalse
) in your array and you want to keep it. But then you can provide your own callback function.