我使用foreach循环从我的数据库中回显出一些值,并用逗号分隔每个值,但最后一个逗号我们如何删除
echo $string='"paymentmethods":';
echo $string='"Bank":[';
$sql2 = "SELECT * FROM paymentmethods where cid=587 ";
$query = $this->db->query($sql2);
foreach ($query->result() as $row){
echo '{';
echo $string = 'accname:'.$row->acc.',' ;
echo $string = 'country:'.$row->IBAN.',' ;
echo $string = 'Iban:'.$row->Bankname.',' ;
echo $string = 'Bankname:'.$row->Bankname.',' ;
echo $string = '},';
}
echo $string = '],';
"paymentmethods":"Bank":[{accname:0034430430012,country:AE690240002520511717801,Iban:ARABIC BANK NAME,Bankname:ARABIC BANK NAME,},{accname:0506796049,country:DE690240002520511717801,Iban:ARABIC BANK NAME,Bankname:ARABIC BANK NAME,},]
这里看到逗号在名称结束后重复。以及括号结束后
答案 0 :(得分:0)
逗号就在那里因为你已经在你的代码中写了它。将行更改为:
// same as above
echo $string = 'Bankname:'.$row->Bankname. ;
echo $string = '}';
}
echo $string = ']';
答案 1 :(得分:0)
执行此操作的常用方法是这样做:
$sep = '';
$result = '';
foreach($myarray as $value) {
$result .= $sep.$value;
$sep = ',';
}
这样,你就没有开头或结尾的逗号。
但正如评论所说,你正在做一个" json_encode"你自己......你应该使用json_encode / decode。