Created
June 24, 2023 11:19
-
-
Save awesomephant/63078af62a5e0635347153e170cc62eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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