Php 用逗号分隔

Php 用逗号分隔,php,csv,Php,Csv,嘿,我有这个 $following_user_id .= $row['following_user_id']; 我得到 44443344330 然后我使用内爆()函数并用逗号分隔 44,44,33,44,33,0, 但我不想要最后一个数字上的最后一个逗号 这可能吗 内爆不应在该字符串的末尾插入逗号。您确定数组序列末尾没有空字符串吗 无论哪种方式,要修复现有字符串,只需去掉字符串的最后一个字符: $concatUserIds = "44,44,33,44,33,0,"; $concatUs

嘿,我有这个

$following_user_id .= $row['following_user_id'];
我得到

44443344330
然后我使用内爆()函数并用逗号分隔

44,44,33,44,33,0, 
但我不想要最后一个数字上的最后一个逗号


这可能吗

内爆不应在该字符串的末尾插入逗号。您确定数组序列末尾没有空字符串吗

无论哪种方式,要修复现有字符串,只需去掉字符串的最后一个字符:

$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
此外,如果您不打算使用非逗号分隔的数字集,为什么不在每次添加用户id时添加一个逗号。这样您甚至不必使用内爆函数。

检查内爆:

代码示例:我假设您使用某种循环

$arrUsers = new array();

... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code .. 
$following_user_id = impload(",", $arrUsers); 

例如,尝试使用数组

<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);

您可以将字符串拆分为一个字符数组,然后对数组进行内爆

$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);

echo implode( ',', $array );

将数据收集到字符串数组中,并使用内爆函数:

$uids = array();
while($row = mysql_fetch_assoc($result)){
    array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
这对我很有用:

<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>


如何使用内爆。显示代码。你确定要对该代码使用内爆吗?通常情况下,当正在内爆的数组末尾有一个空字符串时会发生这种情况。可能是重复的井。你能为我重写代码吗?你能用逗号将它们内爆吗?因为结果是44443344330,我已经有了,但我不明白,这就是为什么我发布了这个,我就是不能用一个变量。数组推送($arruusers,$row['following_user_id');/*然后,在它们全部加载到数组中后,使用*/$following\u user\u id=inpload(“,”,$arrcusers)使用inplode;是一个使用答案的示例Roland gaveRoland向您指出了该链接,我刚刚添加了一个使用该链接信息的代码示例。
infrade()
只是答案的一部分。询问者还需要知道如何将字符串转换为数组。您还可以执行以下操作:$following_user_id.=','$行['following_user_id'];这是真的,但是如果用户这样做的话,应该被打屁股,因为这样做的话,内爆是存在的,更干净,不是黑客。但你是对的,那将是另一种选择。rtrim($以下为用户id,“”)
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>