-3

I have a string which is concatenated together that needs to be comma separated. I also want to add the work "and" just before the last item.

?php
   $general = get_field('general');
   $language_inclusions = $general['language_inclusions'];
   

   foreach ($language_inclusions as $language) {
   echo $language['language']; 

   } ?>

This is my output

EnglishPortugueseChinese

expected result should be English, Portuguese **and** Chinese

raja
  • 25
  • 5

1 Answers1

-1

There are implode method available for join two values of array.

function join_string($array){
    #Filter array for remove null value from array
    $array = array_filter($array);

    #remove the last value and store it in variable
    $last_value = array_pop($array);

    return implode(', ',$array)." and ".$last_value;
}
  • Already covered by https://stackoverflow.com/a/25057951/2943403 and https://stackoverflow.com/a/52891549/2943403 – mickmackusa Apr 26 '22 at 06:53