我在html中有以下代码
<? foreach($tst as $test) : ?>
<?=$test->id?>,
<? endforeach ?>
Run Code Online (Sandbox Code Playgroud)
这将导致test1,test2,test3,
如何避免使用简单方法的最后一个逗号.我不能在html中使用复杂的代码
<? $i = 0 ;?>
<? foreach($tst as $test) : ?>
<?=$test->id?>,
<? endforeach ?>
<? $i++ ;?>
<? if($i != count($tst)) :?>
,
<?endif;?>
<? endforeach;?>
Run Code Online (Sandbox Code Playgroud)
请帮忙 :)
使用implode临时数组:
<?php
$a= array();
foreach($tst as $test) {
$a[]= $test->id;
}
echo(implode(', ', $a));
?>
Run Code Online (Sandbox Code Playgroud)