Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a array which i generated by values in a database, the example is below:

$addressarray = array($results['client']->client_city, $results['client']->client_county, $results['client']->client_postcode);

The values are entered by the user using a from, the above array works and the correct values are placed into it, however sometimes the user may not enter the clients county, so therefore

$results['client']->client_county

may be blank. I call the array with this.

$address = implode("\n  ", $addressarray);

Now this is the part that i think need fixing, obviously if all the fields have a value then they are displayed with line breaks, but if like i mentioned above the county is blank it will stll output a line break so you will get:

city

postcode

but what i want is

city
postcode

I guessing the

\n

is the issue but am at a blank. any help appreciated.

Ian

share|improve this question

2 Answers

up vote 3 down vote accepted

I think you can use array_filter to your array before use implode() function

$address = implode("\n", array_filter($addressarray));
share|improve this answer
 
wouldn't you need a call back for array_filter to check if the value is empty? or does array_filter do that by default? –  TheSnooker May 31 at 15:04
 
@TheSnookier If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. –  Fabio May 31 at 15:05
 
You Star, Thankyou –  snookian May 31 at 15:05
 
@snookian you are welcome, don't forget to accept and upvote answer to help people in the future with similar problems –  Fabio May 31 at 15:06

try to use array_filter() on the $adressesarray, it filters empty values.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.