Skip to content

Instantly share code, notes, and snippets.

@SleeplessByte
Last active April 28, 2024 09:26
Show Gist options
  • Save SleeplessByte/4514697 to your computer and use it in GitHub Desktop.
Save SleeplessByte/4514697 to your computer and use it in GitHub Desktop.
Creates a human readable list of an array, implodes all items, but combines last two first.
<?php
/**
* Creates a human readable list of an array
*
* @param string[] $ranges array to list items of
* @param string $glue normal glue between items
* @param string $last glue between last two items
*
* @remarks works with 0, 1, 2 or 3+ items
* @returns string 'item1, item2, item3 or item4'
*/
function array_listing( $ranges, $glue = ', ', $last = ' or ') {
array_splice( $ranges, -2, 2, implode( $last, array_slice( $ranges, -2, 2 ) ) );
return implode( $glue , $ranges );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment