src/Utilities/Arrays.php
public static function implodeSmart(array $array, $separator)
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        foreach ($array as &$element) {
            if (is_array($element)) {
                $element = self::implodeSmart($element, $separator);
            }

            if (Regex::startsWith($element, $separator)) {
                $element = substr($element, 1);
            }

            if (Regex::endsWith($element, $separator)) {
                $element = substr($element, 0, -1);
            }
        }

        return implode($separator, $array);
    }
public static function implodeSmart(array $array, $separator)
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        foreach ($array as &$element) {
            if (is_array($element)) {
                $element = self::implodeSmart($element, $separator);
            }

            if (Regex::startsWith($element, $separator)) {
                $element = substr($element, 1);
            }

            if (Regex::endsWith($element, $separator)) {
                $element = substr($element, 0, -1);
            }
        }

        return implode($separator, $array);
    }
public static function implodeSmart ( array $ array , $ separator ) { /*
         * No elements?
         * Nothing to do
         */ if ( empty ( $ array ) ) { return null ; } foreach ( $ array as & $ element ) { if ( is_array ( $ element ) ) { $ element = self :: implodeSmart ( $ element , $ separator ) ; } if ( Regex :: startsWith ( $ element , $ separator ) ) { $ element = substr ( $ element , 1 ) ; } if ( Regex :: endsWith ( $ element , $ separator ) ) { $ element = substr ( $ element , 0 , - 1 ) ; } } return implode ( $ separator , $ array ) ; }
Returns smartly imploded string

Separators located at the beginning or end of elements are removed.
It's required to avoid problems with duplicated separator, e.g. "first//second/third", where separator is a
"/" string.

@param array  $array     The array with elements to implode
@param string $separator Separator used to stick together elements of given array
@return null|string
Returns smartly imploded string