Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove last comma (,) from array?

Tags:

php

I've gone through this address:

Passing an array to a query using a WHERE clause

and found that if I use a join clause to separate values , is also at the end of the array. How can I remove last?

I am using like this

$ttst=array();
$ttst=array(); 
$tt = "SELECT frd_id FROM network WHERE mem_id='$userId'"; 
$appLdone = execute_query($tt, true, "select"); 
foreach($appLdone as $kk=>$applist){ 
    $ttst[] = $applist['frd_id']; 
} 
$result = implode(',', $ttst); 

then also coming last ,

Thanks.


but it doesn't give single quote to each value .

like image 416
RockDance Avatar asked Nov 29 '22 16:11

RockDance


2 Answers

join(',', array_filter($galleries))

array_filter gets rid of empty elements that you seem to have. Of course, you should take care not to have empty elements in there in the first place.

like image 50
deceze Avatar answered Dec 10 '22 08:12

deceze


You could use trim() (or rtrim()):

$myStr = 'planes,trains,automobiles,';
$myStr = trim($myStr, ',');
like image 20
jensgram Avatar answered Dec 10 '22 09:12

jensgram