PHP数组和空白/空值破灭 [英] PHP array and implode with blank/null values

查看:130
本文介绍了PHP数组和空白/空值破灭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我通过在数据库中的值生成一个数组,这个例子如下:

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);

现在这是我认为需要修复,很明显,如果所有字段有一个值那么他们显示有换行符的一部分,但如果像我县级以上提到的是空白的它会stll输出一个​​换行符所以你会得到:

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

但我想要的是

city
postcode

我猜

\n

是问题,但我在一个空白。任何帮助AP preciated。

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

伊恩

推荐答案

我想你可以使用 array_filter 使用前阵列破灭()函数

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

这篇关于PHP数组和空白/空值破灭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆