duangouyan3328 2014-01-24 08:39
浏览 168
已采纳

foreach循环最后迭代问题(最后一个变量)

Quick PHP problem here :

When doing a for each statement I have to echo it in a perticular syntax I.E

|First_Name:bob,jim,alex,gary|Last_Name:Smith,Doe,foo|Age:11,12,13

I have so far manage to achieve this syntax except for the last value of each of the for loop as I get this result

|First_Name:bob,jim,alex,gary,|Last_Name:Smith,Doe,foo,|Age:11,12,13,

so the is an extra comma in every itteration of the second loop.

Is there a way to get rid of the comma for the last value only.

  • 写回答

2条回答 默认 最新

  • dongtie0929 2014-01-24 09:11
    关注

    There is a built-in function for joining array elements, called implode, which I suggest you should use:

    $a = [1, 2, 3];
    $s = implode(",", $a);
    
    // result: "1,2,3"
    

    NB: The short array notation was introduced in PHP 5.4. For older versions, use this line instead to initialize an array:

    $a = array(1, 2, 3);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数
  • ¥15 关于#线性回归#的问题:【统计】回归系数要转化为相关系数才能进行Fisher' Z转化吗(相关搜索:回归模型)
  • ¥100 使用matlab解决含分段变量的优化问题