如何轻松地从数组中删除最后一个逗号? [英] How can I easily remove the last comma from an array?

查看:44
本文介绍了如何轻松地从数组中删除最后一个逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个:

$array = array("john" => "doe", "foe" => "bar", "oh" => "yeah");

foreach($array as $i=>$k)
{
echo $i.'-'.$k.',';
}

回声 "john-doe,foe-bar,oh-yeah,"

如何去掉最后一个逗号?

How do I get rid of the last comma?

推荐答案

或者您可以使用 rtrim 函数为:

Alternatively you can use the rtrim function as:

$result = '';
foreach($array as $i=>$k) {
    $result .= $i.'-'.$k.',';
}
$result = rtrim($result,',');
echo $result;

这篇关于如何轻松地从数组中删除最后一个逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆