Search code examples
phphtmlwordpressstringtags

Print all array values in <span> tags with a trailing comma except no comma after the last value


I am hacking together a theme for Wordpress and I am using the following code to pull out data from a custom field with several values:

$mykey_values = get_post_custom_values('services');
foreach ( $mykey_values as $key => $value ) {
    echo "<span>$value, </span>";
}

I use a comma to separate the results, but I don't want a comma after the last result. How do I get around this?


Solution

  • Best way is with implode:

    echo('<span>' . implode('</span>, <span>', $mykey_values) . '</span>');