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

后端 未结 6 1259
青春惊慌失措
青春惊慌失措 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:53

    $number_list = array(4, 5, 6, 7);    
    $comma_list = strrev(implode(strrev(', and'), explode(strrev(','), strrev(implode(', ', $number_list)), 2)));
    echo 'The list is ' . $comma_list . '.';
    

    Output: The list is 4, 5, 6, and 7.

提交回复
热议问题