RosettaCodeData/Task/Comma-quibbling/PHP/comma-quibbling.php

34 lines
479 B
PHP
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
<?php
2024-04-19 16:56:29 -07:00
function quibble($arr) {
2023-07-01 11:58:00 -04:00
2024-04-19 16:56:29 -07:00
switch (count($arr)) {
2023-07-01 11:58:00 -04:00
2024-04-19 16:56:29 -07:00
case 0:
return '{}';
case 1:
return "{{$arr[0]}}";
default:
$left = implode(', ', array_slice($arr, 0, -1));
$right = array_slice($arr, -1)[0];
return "{{$left} and {$right}}";
}
2023-07-01 11:58:00 -04:00
}
$tests = [
[],
["ABC"],
["ABC", "DEF"],
["ABC", "DEF", "G", "H"]
];
foreach ($tests as $test) {
echo quibble($test) . PHP_EOL;
}
2024-04-19 16:56:29 -07:00
?>