Implode an Array only for non-empty values

Description PHP

Implodes a string with only the pieces of an array that are not empty.

The Code Download

  1. function implodeSupplied($glue,$pieces){
  2.     if (is_array($pieces)){
  3.         $str = '';
  4.         foreach($pieces as $key => $val){
  5.             if (strlen($val) > 0){
  6.                 $str .= $val.$glue;
  7.             }
  8.         }
  9.         return trim($str,$glue);
  10.     } else {
  11.     return $pieces;
  12.     }
  13. }

Credits

Added by JC on 14th January 2008

Comments

There are no comments about this snippet.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment