php - 如何在 PHP 结果表中添加逗号
问题描述
如何使用 PHP 在搜索结果表中添加逗号?下面是我得到的代码。结果在第二列中没有逗号的情况下粘在一起,我尝试添加逗号,例如echo $aaa = $newlist->name.',';
. 它没有用。
<tr>
<th>Class Name </th>
<th>Class Type</th>
<th>Recommended Fitness Level</th>
<th>Location</th>
</tr>
<?php
for ($i = 0; $i < $countposts; $i++) {
$pid = $unique_posts[$i];
$title = get_the_title($pid);
$location = get_field('location', $pid);
$fitness_level = get_field('fitness_level', $pid);
$imp_fitness = implode(',', $fitness_level);
include("locationfile.php");
?>
<tr>
<td><?php echo $title; ?></td>
<td><?php
$term_list = wp_get_post_terms($pid, 'classtypecategory', array("fields" => "all"));
foreach ($term_list as $newlist) {
echo $aaa = $newlist->name . '';
}
?>
</td>
<td><?php echo $imp_fitness; ?></td>
<td><a href="<?php echo $url; ?>" target="_blank"><?php echo $location; ?></a></td>
</tr>
<? } ?>
解决方案
//Method 1:
foreach($term_list as $newlist){
$aaa .= $newlist->name.',';
}
echo $aaa=substr($aaa,0,-1);
//Method 2:
// For implode
foreach($term_list as $newlist){
$text[] = $newlist->name;
}
echo $aaa=implode(',',$text);
试试这个,而不是你的 foreach
推荐阅读
- php - 如何仅显示数据库中 user_id 与 Laravel 中的当前用户相同的不同数据
- python - Python Plotly - 为什么 2 个曲面图显示不同的结果?
- reactjs - Redux Toolkit 中的自定义中间件
- c - C中矩阵动态分配的模块化
- c - Revealer 在活动时隐藏另一个 Revealer (gtk_tree_store) - GTK3 , C
- html - ffmpeg 流桌面代替 -i /dev/video0
- tailwind-css - 如何在tailwindcss中使用带有活动伪类的变换类
- angular - 如何对@viewChild ElementRef 角度进行单元测试
- css - CSS盒装模板不会盒装
- css - 用于移动媒体查询的 Asp.Net Bootstrap 重新格式化表,无需侧滚动