我想在每个项目后面都加上逗号,除了最后一个,最后一个必须是 “和”。
项目1,项目2和项目3
但是项目数量可以是 1 +
所以如果只有一个项目:
项目1
如果有两个项目:
项目1和项目2
如果有三个项目:
项目1,项目2和项目3
如果有四个项目:
项目1,项目2,项目3和项目4
等等等等。
我想在每个项目后面都加上逗号,除了最后一个,最后一个必须是 “和”。
项目1,项目2和项目3
但是项目数量可以是 1 +
所以如果只有一个项目:
项目1
如果有两个项目:
项目1和项目2
如果有三个项目:
项目1,项目2和项目3
如果有四个项目:
项目1,项目2,项目3和项目4
等等等等。
这里有一个函数可以做到这一点,只需传递数组即可。
function make_list($items) {
$count = count($items);
if ($count === 0) {
return '';
}
if ($count === 1) {
return $items[0];
}
return <a rel="nofollow" href="https://php.net/implode">implode</a>(', ', <a rel="nofollow" href="https://php.net/array_slice">array_slice</a>($items, 0, -1)) . ' and ' . <a rel="nofollow" href="https://php.net/end">end</a>($items);
}
Minitech的解决方案一开始看起来很优雅,除了一个小问题,它的输出结果将会是:
var_dump(makeList(array('a', 'b', 'c'))); //Outputs a, b and c
implode()
时,数组切片会将其视为数组的最后一个元素。下面是我拥有的实现,也是正确的(同样有争议)格式化列表:class Array_Package
{
public static function toList(array $array, $conjunction = null)
{
if (is_null($conjunction)) {
return implode(', ', $array);
}
$arrayCount = count($array);
switch ($arrayCount) {
case 1:
return $array[0];
break;
case 2:
return $array[0] . ' ' . $conjunction . ' ' . $array[1];
}
// 0-index array, so minus one from count to access the
// last element of the array directly, and prepend with
// conjunction
$array[($arrayCount - 1)] = $conjunction . ' ' . end($array);
// Now we can let implode naturally wrap elements with ','
// Space is important after the comma, so the list isn't scrunched up
return implode(', ', $array);
}
}
// You can make the following calls
// Minitech's function
var_dump(makeList(array('a', 'b', 'c')));
// string(10) "a, b and c"
var_dump(Array_Package::toList(array('a', 'b', 'c')));
// string(7) "a, b, c"
var_dump(Array_Package::toList(array('a', 'b', 'c'), 'and'));
string(11) "a, b, and c"
var_dump(Array_Package::toList(array('a', 'b', 'c'), 'or'));
string(10) "a, b, or c"
没有别的意思,只是想提出这一点。
function conjoinList($items, $conjunction='and', $oxford=false) {
$count = count($items);
if ($count === 0){
return '';
} elseif ($count === 1){
return $items[0];
} elseif ($oxford && ($count === 2)){
$oxford = false;
}
return implode(', ', array_slice($items, 0, -1)) . ($oxford? ', ': ' ') . $conjunction . ' ' . end($items);
}
$items = array("Item 1", "Item 2", "Item 3", "Item 4");
$item = glueItems($items);
function glueItems($items) {
if (count($items) == 1) {
$item = implode(", ", $items);
} elseif (count($items) > 1) {
$last_item = array_pop($items);
$item = implode(", ", $items) . ' and ' . $last_item;
} else {
$item = '';
}
return $item;
}
echo $item;
我的函数基于其他人的代码,但我觉得更加简洁。此外,它总是添加牛津逗号,因为实际上并没有关于是否使用它正确的“争议”或辩论。
//feed in an array of words to turn it into a written list.
//['bacon'] turn into just 'bacon'
//['bacon','eggs'] turns into 'bacon and eggs'
//['bacon','eggs','ham'] turns into 'bacon, eggs, and ham'
function writtenList($items) {
//nothing, or not an array? be done with this
if (!$items || !is_array($items)) return '';
//list only one or two items long, this implosion should work fine
if (count($items)<=2) {
return implode(' and ',$items);
//else take off the last item, implode the rest with just a comma and the remaining item
} else {
$last_item = array_pop($items);
return implode(", ",$items).', and '.$last_item;
}
}
您可以使用逗号将前X-1个项目合并,然后使用“和”添加最后一个项目。
如果它是一个数组,只需使用implode(...)
例如:
$items = array("Item 1", "Item 2", "Item 3", "Item 4");
$items[count($items) - 1] = "and " . $items[count($items) - 1];
$items_string = implode(", ", $items);
echo $items_string;