Home Forums General Issues Remove comma from last checkbox item


Remove comma from last checkbox item

  • Hi there,

    I’m sure this is quite trivial for your but I’m not a coder eh eh…

    I’ve manage to achieve what I want but I think it can be improved.

    I have a checkbox field, and I want to retrieve both label and value and want to separate them with a comma. This part is working fine. However, I want to remove the comma from the last item. I can add the comma in a span and then target the last span with CSS and remove it but I want it to be better.

    This is what I came up with by reading the docs and searching other topics

    $plans = get_field('plans');
    if ( $plans) : ?>
    <?php foreach( $plans as $plan ): ?>
           <a target="_blank" title="<?php echo $plan['label']; ?>" href="<?php echo $plan['value']; ?>"><?php echo $plan['label']; ?></a><span>,</span>
        <?php endforeach; ?>
    <?php endif; ?>
    $plans = get_field('plans');
    if ($plans) {
      $lines = array();
      foreach ($plans as $plan) {
        $lines[] = '<a target="_blank" title="'.$plan['label'].
                   '" href="'.$plan['value'].'">'.$plan['label'].'</a>';
      } // end foreach plan
      echo implode(', ', $lines);
    } // end if plans
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.