Implode and remove empty values

Posted by binoy on Stack Overflow See other posts from Stack Overflow or by binoy
Published on 2010-05-20T12:14:05Z Indexed on 2010/05/20 12:20 UTC
Read the original article Hit count: 237

Filed under:

Hi,

I have an array like this

$arr = array('name' => 'John',
             'address' => 'IInd Street',
             'state' => '',
             'country' => 'US');

I want to display the values as

John,
IInd Street,
US

(ie, If any value is empty, I don't want to display comma and break over there)

I could able to display this with the following code

$values = implode(',', $arr);
echo $str = implode(',<br />',array_filter(explode(',', $values)));

Is there any better way to do this ?

© Stack Overflow or respective owner

Related posts about php