-
last of the implodes
28 Oktsometimes implode is not enough. if you’re working with text that users should read and you have a set (read array) of items that you want to join to a single string you probably think about something like “merge all the items with a comma and the last one with something different”. unfortunately php doesn’t have that built-in even though it’s a pretty common thing to do in templates.
snippets to the win. here’s what does the job (and is pretty fast):
function lastImplode($glue, $lastGlue, $data) { return implode($glue, array_slice($data, 0, -1)) . $lastGlue . end($data); }Usage:
lastImplode(", ", " and ", array("Admin", "CEO", "Misc"));
