php数组循环输出的时候,怎样去掉最后的逗号?
<?php$arr=array("张三","李四","王五","赵六");$arr1="";foreach($arras$key=>$value){$arr1.='"'....
<?php
$arr = array("张三","李四","王五","赵六");
$arr1="";
foreach($arr as $key=> $value)
{
$arr1.='"'.$value.'"'.',';
}
echo $arr1;
?>
输出结果是:
字符串连接之后,我不想输出“赵六”后面的逗号,怎样实现? 展开
$arr = array("张三","李四","王五","赵六");
$arr1="";
foreach($arr as $key=> $value)
{
$arr1.='"'.$value.'"'.',';
}
echo $arr1;
?>
输出结果是:
字符串连接之后,我不想输出“赵六”后面的逗号,怎样实现? 展开
2个回答
展开全部
1、在循环体外去掉即可:
{php $modulShow = substr($modulShow,0,-1)}
2、用if语句判断一下:
public class If {
public static void main(String[] args) {
String[] hobbys = { "sports","game","movie"};
System.out.println("循环输出数组中的元素的值");
for (int i = 0;i<hobbys.length;i++){
if(i<(hobbys.length-1)){
System.out.print(hobbys[i]+",");
}else{
System.out.print(hobbys[i]+"");
}
}
}
}
{php $modulShow = substr($modulShow,0,-1)}
2、用if语句判断一下:
public class If {
public static void main(String[] args) {
String[] hobbys = { "sports","game","movie"};
System.out.println("循环输出数组中的元素的值");
for (int i = 0;i<hobbys.length;i++){
if(i<(hobbys.length-1)){
System.out.print(hobbys[i]+",");
}else{
System.out.print(hobbys[i]+"");
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询