7

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

2 Answers 2

23

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

$address = implode("\n", array_filter($addressarray));
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@TheSnookier If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.