逗号分隔的列表,从阵列"和"最后一个元素之前 [英] Comma separated list from array with "and" before last element

查看:101
本文介绍了逗号分隔的列表,从阵列"和"最后一个元素之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组( $ number_list ),有值的动态生成的列表。会有阵列中的至少1值和不超过4个。

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.

目前,我用这个有一个逗号分隔的列表的一个很好的方式...

Currently, I have a nice way of having a comma separated list using this...

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

不过,我想保持与英国惯例,并有最后一个元素前的和字。所以,让我们说$ number_list包含值4,5,6,7,我会想呼应的声明一样,这份名单是4,5,6, 7。

任何想法如何获取在那里?

Any ideas how to get that and in there?

推荐答案

尝试 array_pop()来获得最后一个元素,那么 array_push 来修改和推背的元素:

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

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

然后,你可以做你的破灭:

Then you can do your implode:

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

这篇关于逗号分隔的列表,从阵列"和"最后一个元素之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆