PHP array and implode with blank/null values

我与影子孤独终老i 提交于 2019-12-01 17:12:39

问题


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


回答1:


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

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



回答2:


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



来源:https://stackoverflow.com/questions/16860270/php-array-and-implode-with-blank-null-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!