Join (implode) an Array of String Values with Formatting

In programming, I often need to join values together from an array. Typically this will be to do something on the backend, like join a list of integers together in a comma-delimited string for use in a SQL statement. For those times, the native join/implode functions work fine. Occasionally however, we need to finesse the resulting string to look a little more readable. I wrote a small function to do so. It’s called pretty_join()

[gist id=1021214 file=pretty_join.php]

Given an array of 0 or more values, pretty_join() will concatenate all of the values together using a common delimiter (a comma by default) except for the last two values which will be joined by the final separator (an ampersand by default).

Examples:

[gist id=1021214 file=pretty_join_examples.php]

One thought on “0

  1. Nice post! FYI, your tweet button doesn't appear to be working… at least not for me!

Comments are closed.

Cooking is like love. It should be entered into with abandon or not at all.

— Harriet Van Horne, Vogue Magazine, 1956