Support

Account

Home Forums Front-end Issues Implode Post Object with "," and add "and" at last word Reply To: Implode Post Object with "," and add "and" at last word

  • I don’t see why it wouldn’t work in principle as long as you have at least 2 items in the array. But it will fall apart when there are none or 1. Although I do not understand your purpose for using array_filter() and filtering all the values through strlen() as this would return an array of the string lengths of the elements.

    
    $development = the_sub_field('development', $post_id);
    $output = false;
    if (count($development) == 1) {
      $output = $development[0];
    } elseif (count($development) > 1)  {
      $last  = array_slice($development, -1);
      $first = join(', ', array_slice($development, 0, -1));
      $both  = array_merge(array($first), $last);
      $output = join(' and ', $both);
    }
    if ($output) {
      echo $output;
    }