/**
* @snippet Describe your snippet’s purpose here
*/
foreach($results as $result){
echo $result->name.’,’;
}
Which echos out

result,result,result,result,
I just need to kill that pesky last comma.

Any help would be great πŸ™‚

Cheers
β€”
$resultstr = array();
foreach ($results as $result) {
$resultstr[] = $result->name;
}
echo implode(β€œ,”,$resultstr)
——–
2. Echo | only if not last item
$s = ”;
$n = count($results);
foreach ($results as $i => $result) {
$s .= $result->name;
if (($i+1) != $n) $s .= β€˜|’;
}
echo $s;
β€”β€”β€”β€”
3. Load to array and then implode
$s = array();
foreach ($results as $result) {
$s[] = $result->name;
}
echo implode(β€˜|’, $s);
4. Concat to string then cut last | (or rtrim it)
$s = ”;
foreach ($results as $result) {
$s .= $result->name . β€˜|’;
}
echo substr($s, 0, -1); # or # echo rtrim($s, β€˜|’);
5. Concat string using array_map()
echo implode(β€˜|’, array_map(function($result) { return $result->name; }, $results));
β€”β€”β€”β€”-
$posts = get_field(β€˜it24_related_products’);
$ids;
foreach( $posts as $p ) {

$ids .= $p.”,”;
}
$str = rtrim($ids,’,’);
print_r($str);
echo β€˜

NΓͺn sα»­ dα»₯ng cΓΉng sαΊ£n phαΊ©m

β€˜;
echo do_shortcode(β€˜[pt_view id=”07da3935gf” post_id=”2442,2435,2408,1928β€³]’);
?>

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x