Comma separated list from array with “and” before last element

后端 未结 6 1256
青春惊慌失措
青春惊慌失措 2020-12-11 05:43

I have an array ($number_list) that has a dynamically generated list of values. There will be at least 1 value in the array and no more than 4.

Currentl

6条回答
  •  心在旅途
    2020-12-11 05:59

    Try array_pop() to get the last element, then array_push to modify and push the element back:

    http://php.net/manual/en/function.array-pop.php

    Something like

    $last_element = array_pop($number_list);
    array_push($number_list, 'and '.$last_element);
    

    Then you can do your implode:

    $comma_list = implode(', ', $number_list);
    

提交回复
热议问题