src/String/StringTools.php
public static function implodeIgnoringBlanks($glue, $array, $itemCallback = null, $keysToInclude = null)
    {
        $array = array_filter($array);

        if (!empty($itemCallback)) {
            foreach ($array as $key => $value) {
                $array[$key] = $itemCallback($value);
            }
        }

        $keys = [$keysToInclude];

        for ($i = 4; $i < func_num_args(); $i++) {
            $keys[] = func_get_arg($i);
        }

        $keys = array_filter($keys);

        $string = "";

        if (count($keys)) {
            foreach ($keys as $key) {
                if (!empty($array[$key])) {
                    $string .= $array[$key] . $glue;
                }
            }
            if (strlen($string) >= strlen($glue)) {
                $string = substr($string, 0, strlen($string) - strlen($glue));
            }
        } else {
            $string = implode($glue, $array);
        }

        return $string;
    }
public static function implodeIgnoringBlanks($glue, $array, $itemCallback = null, $keysToInclude = null)
    {
        $array = array_filter($array);

        if (!empty($itemCallback)) {
            foreach ($array as $key => $value) {
                $array[$key] = $itemCallback($value);
            }
        }

        $keys = [$keysToInclude];

        for ($i = 4; $i < func_num_args(); $i++) {
            $keys[] = func_get_arg($i);
        }

        $keys = array_filter($keys);

        $string = "";

        if (count($keys)) {
            foreach ($keys as $key) {
                if (!empty($array[$key])) {
                    $string .= $array[$key] . $glue;
                }
            }
            if (strlen($string) >= strlen($glue)) {
                $string = substr($string, 0, strlen($string) - strlen($glue));
            }
        } else {
            $string = implode($glue, $array);
        }

        return $string;
    }
public static function implodeIgnoringBlanks ( $ glue , $ array , $ itemCallback = null , $ keysToInclude = null ) { $ array = array_filter ( $ array ) ; if ( ! empty ( $ itemCallback ) ) { foreach ( $ array as $ key => $ value ) { $ array [ $ key ] = $ itemCallback ( $ value ) ; } } $ keys = [ $ keysToInclude ] ; for ( $ i = 4 ; $ i < func_num_args ( ) ; $ i ++ ) { $ keys [ ] = func_get_arg ( $ i ) ; } $ keys = array_filter ( $ keys ) ; $ string = "" ; if ( count ( $ keys ) ) { foreach ( $ keys as $ key ) { if ( ! empty ( $ array [ $ key ] ) ) { $ string .= $ array [ $ key ] . $ glue ; } } if ( strlen ( $ string ) >= strlen ( $ glue ) ) { $ string = substr ( $ string , 0 , strlen ( $ string ) - strlen ( $ glue ) ) ; } } else { $ string = implode ( $ glue , $ array ) ; } return $ string ; }
Removes all blanks in an array and merges them into a string

@param string $glue String to include between each element
@param array $array Array containing elements to be imploded
@param string $itemCallback A transformation closure to call on each element of the array
@param string $keysToInclude,... Keys of the array elements to include - if not supplied, all elements will be used
@return string String of concatenated elements
Removes all blanks in an array and merges them into a string