开发者

separate with commas

hey there I have this,

$following_user_id .= $row['following_user_id'];

and I get

44443344330

then I use the implode() function and seperate with commans

44,44,33,44,33,0, 

but I don't want the last comma on th开发者_开发问答e last number?

Is this possible?


$following_user_ids = array();

//loop this:
$following_user_ids[] = $row['following_user_id'];

$user_ids_string = implode(',',$following_user_ids);


You can split the string into an array of characters, then implode the array.

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

echo implode( ',', $array );


Collect your data into an array of strings and use the implode function:

$uids = array();
while($row = mysql_fetch_assoc($result)){
    array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);


Check implode: http://php.net/manual/en/function.implode.php

Code example: I'm assuming your using some sort of loop?

$arrUsers = new array();

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


Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?

Either way, to fix the string you have, just get rid of the last character of the string:

$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);

Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.


This works for me:

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


Try using arrays, example

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜