用逗号分隔的字符串连接

时间:2022-01-26 00:20:20

How can I get a string from database and concat it with another string seperated by a comma

如何从数据库中获取字符串并将其与另一个用逗号分隔的字符串连接起来

If I have a string such as this "2, 3" in the database. I am getting the string from:

如果我在数据库中有一个像这样的“2,3”的字符串。我从以下字符串获取:

$item->options

Now I have an array which I convert into a string seperated by commas like this:

现在我有一个数组,我将其转换为由逗号分隔的字符串,如下所示:

implode(',', $request->options);

Now my question is how can I put those two strings together seperated by a comma? I tried using + but it is adding the strings together

现在我的问题是如何将这两个字符串放在一起用逗号分隔?我尝试使用+,但它将字符串添加到一起

2 个解决方案

#1


2  

echo $item->options . ',' . implode(',', $request->options);

#2


1  

You concatenate in PHP using the period .; if you want to concatenate $string1 and $string2 separated by a comma, it would be

你使用句点在PHP中连接。如果你想用逗号分隔$ string1和$ string2,那就是

$concatenated_string = $string1 . ',' . $string2;

#1


2  

echo $item->options . ',' . implode(',', $request->options);

#2


1  

You concatenate in PHP using the period .; if you want to concatenate $string1 and $string2 separated by a comma, it would be

你使用句点在PHP中连接。如果你想用逗号分隔$ string1和$ string2,那就是

$concatenated_string = $string1 . ',' . $string2;