php 如何从数组中删除最后一个逗号(,)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6368075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:09:24  来源:igfitidea点击:

How to remove last comma (,) from array?

php

提问by RockDance

I've gone through this address:

我已经通过这个地址:

Passing an array to a query using a WHERE clause

使用 WHERE 子句将数组传递给查询

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?

并发现如果我使用 join 子句来分隔值,也是在数组的末尾。我怎样才能删除最后?

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 .

但它不会为每个值提供单引号。

回答by deceze

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

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

array_filter摆脱你似乎拥有的空元素。当然,您首先应该注意不要在其中包含空元素。

回答by jensgram

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

您可以使用trim()(或rtrim()):

$myStr = 'planes,trains,automobiles,';
$myStr = trim($myStr, ',');

回答by Shef

$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,',');

回答by The Mask

$arr = array(...); 
$last = end($arr); 
unset($last); 

回答by Mattis

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

如果尾随的“,”是其自身的数组元素,则使用array_pop(),否则使用rtrim()

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

Gives:

给出:

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

数组( [0] => 一个1=> 两个2=> 3 )

回答by Steward Godwin Jornsen

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

不需要长度。只需从开始到(最后一个字符 - 1)

$finalResult = $str = substr($result, 0, -1);

回答by Henrik Erlandsson

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

使用 implode() 代替!请参阅此处的示例 #1 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 notethat if some more values in the array are sometimes empty you will probably have to write a foreachloop and build the string.

这应该为您提供围绕字符串的单引号,但请注意,如果数组中的更多值有时为空,您可能必须编写一个foreach循环并构建字符串。