Grow your CSS skills. Land your dream job.

Last updated on:

Concatenate Array for Human Reading

function concatenate($elements, $delimiter = ', ', $finalDelimiter = ' and ') {
       $lastElement = array_pop($elements);
       return join($delimiter, $elements) . $finalDelimiter . $lastElement;
}

Usage

$array = array('John', 'Mary', 'Ishmal');
echo concatenate($array);
// outputs "John, Mary and Ishmal"

Provide second parameter, a string, for a delimiter other than a comma.

Comments

  1. Permalink to comment#

    Thanks, this solution is better than my complicated loop hack.

  2. Shez
    Permalink to comment#

    why not just use implode? implode(“delimiter”, array); :s

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
    ``` 
    <div>
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
    </div>
    ```
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".