Skip to content

Instantly share code, notes, and snippets.

@aklump
Last active November 8, 2025 17:24
Show Gist options
  • Select an option

  • Save aklump/84ab03b9f16c4bda4bfb1ab2d91067bb to your computer and use it in GitHub Desktop.

Select an option

Save aklump/84ab03b9f16c4bda4bfb1ab2d91067bb to your computer and use it in GitHub Desktop.
csv using "and"
<?php
/**
* A class that formats an array of items into a human-readable string.
*
* When the class is invoked as a callable, it formats the provided array
* by joining all elements with commas and adding "and" before the last item.
* This is typically used for creating natural language representations of lists.
*
* Methods:
*
* - __invoke(array $items): Converts an array into a readable string format.
*/
class HumanList {
public function __invoke(array $items): string {
$last = array_pop($items);
$items_string = implode(', ', $items);
return sprintf('%s and %s', $items_string, $last);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment