2

Possible Duplicate:
PHP, Echoing an array into a string of comma seperated values

This seems like it should be fairly straight-forward for experienced PHP coders... which I'm not.

This is what I have so far:

<?php echo $cfs->get_labels('name'); ?>: <?php $values = $cfs->get('name');
    foreach($values as $value => $label) {echo $value . ', ';}
?>

How could I avoid having a comma printed on the last value in something like this?

I've referred to this and similar, but can't seem to crack it.

2
  • I'm pretty sure this has been asked before. Try using the search feature next time. Commented Oct 9, 2012 at 10:23
  • i did. found plenty, as added in the link in the original post. Commented Oct 9, 2012 at 10:25

1 Answer 1

9

You can use echo implode(',', $values); instead of the foreach loop.

EDIT: whoops, I just noticed you output the keys, so for that you can use:

echo implode(',', array_keys($values));

Sign up to request clarification or add additional context in comments.

3 Comments

<?php echo $cfs->get_labels('name'); ?>: <?php $values = $cfs->get('name'); echo implode(',', array_keys($values));} ?> like so then? anyone care to help me spot the syntax error? thanks.
The syntax error is the last curly bracket
just caught it as well. thank you @Lex!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.