我在 html 中有以下代码
<? foreach($tst as $test) : ?>
<?=$test->id?>,
<? endforeach ?>
这将导致 作为测试1,测试2,测试3,
如何在简单方法中避免最后一个逗号。我不能在 html 中使用复杂的代码,例如
<? $i = 0 ;?>
<? foreach($tst as $test) : ?>
<?=$test->id?>,
<? endforeach ?>
<? $i++ ;?>
<? if($i != count($tst)) :?>
,
<?endif;?>
<? endforeach;?>
请帮忙:)
最佳答案
在临时数组上使用implode
:
<?php
$a= array();
foreach($tst as $test) {
$a[]= $test->id;
}
echo(implode(', ', $a));
?>
关于php字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1838350/