php字符串操作

标签 php string

我在 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/

相关文章:

php - notepad++中黄色和紫色的高亮是什么意思?

php - 我应该为 SEO 转换动态网址吗?

php - 为什么我的 Javascript 警报没有显示?

php - 防止 Symfony2 中的同时用户 session

java - 从逗号分隔的字符串中读取=之后的值

sql - Postgresql 函数自动将 's' 字符添加到字符串的末尾

php - 连接 SQL 查询在 1 个表中具有单个 id/行,但链接到另一个表中的多个行(相同 id)?

c++ - 在 C++ 中混淆 char 数组和指针

php - Levenshtein - 分组酒店名称

c++ - 读取包括空字节在内的所有字符