4

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 .

Community
  • 1
  • 1
RockDance
  • 91
  • 1
  • 1
  • 8

7 Answers7

10
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.

deceze
  • 491,798
  • 79
  • 706
  • 853
8

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

$myStr = 'planes,trains,automobiles,';
$myStr = trim($myStr, ',');
jensgram
  • 30,081
  • 6
  • 79
  • 95
4
$str = "orange,banana,apple,";
$str = rtrim($str,',');

This will result in

$str = "orange,banana,apple";

Update

Based on your situation:

$result = implode(',', $ttst);
$result = rtrim($result,',');
Shef
  • 43,457
  • 15
  • 77
  • 89
0

No need for length. Just take from beginning to the (last character - 1)

$finalResult = $str = substr($result, 0, -1);
Steward Godwin Jornsen
  • 1,161
  • 1
  • 6
  • 13
0
$arr = array(...); 
$last = end($arr); 
unset($last); 
The Mask
  • 16,429
  • 36
  • 107
  • 177
0

If the trailing "," is an array element of itself, use array_pop(), else, use rtrim()

$array = array('one','two',3,',');
array_pop($array);
print_r($array);

Gives:

Array ( [0] => one 1 => two 2 => 3 )

Mattis
  • 4,888
  • 3
  • 32
  • 49
-1

Use implode() instead! See Example #1 here http://php.net/manual/en/function.implode.php

Edit:

I think a quick fix would be (broken down in steps):

$temp = rtrim(implode(',', $ttst), ','); //trim last comma caused by last empty value
$temp2 = "'"; //starting quote
$temp2 .=  str_replace(",", "','", $temp); //quotes around commas
$temp2 .= "'"; //ending quote
$result = $temp2; // = 'apples','bananas','oranges'

This should give you single-quote around the strings, but note that if some more values in the array are sometimes empty you will probably have to write a foreach loop and build the string.

Henrik Erlandsson
  • 3,721
  • 5
  • 42
  • 61
  • 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 , – RockDance Jun 16 '11 at 06:53
  • 1
    @RockDance, you should add that code (properly formatted) to the question. – Mischa Jun 16 '11 at 06:56
  • @deceze, ok, I've always used implode and never join, and knew that implode only puts commas *between* the items. It's apparent now that he has 1 (or more) empty string results in the array, but it was hard to make that out before he edited the question. – Henrik Erlandsson Jun 16 '11 at 14:06