Skip to content

Instantly share code, notes, and snippets.

@awesomephant
Created June 24, 2023 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesomephant/63078af62a5e0635347153e170cc62eb to your computer and use it in GitHub Desktop.
Save awesomephant/63078af62a5e0635347153e170cc62eb to your computer and use it in GitHub Desktop.
<?php
function array_to_sentence($arr)
{
$s = "";
if (count($arr) === 1) {
$s = $arr[0];
} elseif (count($arr) === 2) {
$s = $arr[0] . " and " . $arr[1];
} else {
$slice = array_slice($arr, 0, count($arr) - 1);
$last = $arr[count($arr) - 1];
$s = join(", ", $slice) . ", and " . $last;
}
return $s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment