简体   繁体   English

如果数组中的值为空,则以逗号分隔

[英]comma separated if value in array is empty

I am stuck with a problem, I have fetched some values from a MySQL query and put them in to an array like so: 我遇到了一个问题,我从MySQL查询中获取了一些值并将它们放入一个数组中,如下所示:

$add1 = $location->address1;
$add2 = $location->address2;
$twn = $location->town;
$pcode = $location->postcode;
$latitude = $location->lat;
$longitude = $location->lng;

$fullAddress = [$add1, $add2, $twn, $pcode];

$string = rtrim(implode(',', $fullAddress), ',');

echo $string;

so that I can echo out a users address. 这样我就能回应出用户地址。 The problem I am getting is that even if one of these values does not exist (and some don't because they are not all required fields), the comma is still echoed to the screen like: 我得到的问题是,即使其中一个值不存在(有些不是因为它们不是所有必需的字段),逗号仍然会回显到屏幕,如:

add1,, town, br2 5lp 

because there is an empty value in the database. 因为数据库中有一个空值。

What I want to achieve is something like: 我想要实现的是:

add1, town, br2 5lp 

if the second part of the address is missing. 如果地址的第二部分丢失了。

Can anyone help me figure this out? 任何人都可以帮我解决这个问题吗?

try this. 尝试这个。

$fullAddress = [$add1, $add2, $twn, $pcode];
$string = implode(',', array_filter($fullAddress, 'strlen'));
echo $string;
The problem I am getting is that even if one of these values does not
exist (and some dont because they are not all required fields), 
the comma is still echoed to the screen 

That is what the implode function in php is supposed to do. 这就是php中的implode函数应该做的事情。 If you want a different behavior, you will have to either change the way you create your CSV string or do some extra processing on the information you obtain from the implode function. 如果您想要不同的行为,则必须更改创建CSV字符串的方式,或者对从implode函数获取的信息执行一些额外处理。

So use a for loop or a foreach loop to go through the address array. 因此,使用for循环或foreach循环来遍历地址数组。 A for loop is faster than a foreach loop. for循环比foreach循环快。

Using Foreach: 使用Foreach:

$add1 = $location->address1;
$add2 = $location->address2;
$twn = $location->town;
$pcode = $location->postcode;
$latitude = $location->lat;
$longitude = $location->lng;

$fullAddress = [$add1, $add2, $twn, $pcode];

$string = "";
foreach($fullAddress as $value)
{
     if(!empty($value))
          $string .= $value.", ";
}

$string = rtrim($string, ", ");

echo $string;

You can do some extra processing on the created csv string of your own solution by maybe doing a str_replace() of all occurances of ,, with , . 您可以对自己的解决方案的已创建的csv字符串执行一些额外的处理,方法是执行所有出现的str_replace() ,, with , . This could be dangerous because if any of the values in your $fullAddress array contain a ,, as a valid string then that will also be replaced too with , . 这可能是危险的,因为如果你的任何$ fullAddress数组中的值的含有,,作为然后一个有效的字符串也将被更换过,

What you need is to assure array $fullAddress have no empty value, so php built-in function array_filter make this purpose very easy. 你需要的是确保数组$fullAddress没有空值,所以php内置函数array_filter使这个目的非常简单。

Just change your last codes to: 只需将您的上一个代码更改为:

$string = implode(',', array_filter($fullAddress));
echo $string;

If the second parameter of array_filter is not supplied, all entries of array equal to FALSE will be removed. 如果未提供array_filter的第二个参数, array_filter所有等于FALSE的数组条目。 So just simply use array_filter($fullAddress) it make the code more clear and simple. 因此,只需使用array_filter($fullAddress)它就可以使代码更加清晰简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM