I am really new with PHP and all I have to do is to take all selected values and print them out separating them with comma, however, I need to drop last comma after last item in list.
<?php $favoriteFruit = get_post_meta($post->ID, 'Favorite Fruit', false) ?>
<?php if ($favoriteFruit != null): ?>
<attribute key="FavoriteFruit"
value="<?php foreach($favoriteFruit as $fruit){
echo $fruit.',';
} ?>"
/>
<?php endif ?>
This one prints out all items in list and doesn’t drop last comma. Any ideas?
how about simply imploding the array, using a comma as delimiter:
Just for fun: since people pointed out you can keep the loop and use
rtrim, and -thankfuly, advised against it. Here’s another needlessly complex and absurd approach that works, too:Whatever you do, don’t use this code.