使用空/空值的PHP数组和内爆

时间:2022-06-26 11:48:20

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

这些值由用户使用from输入,上面的数组可以工作,正确的值被放置在其中,但是有时用户可能不会进入客户县,因此

$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 个解决方案

#1


15  

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

我认为在使用内爆()函数之前,可以对数组使用array_filter

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

#2


3  

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

尝试在$adressesarray上使用array_filter(),它过滤空值。更多array_filter()

#1


15  

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

我认为在使用内爆()函数之前,可以对数组使用array_filter

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

#2


3  

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

尝试在$adressesarray上使用array_filter(),它过滤空值。更多array_filter()