src/Utils.php
public static function joinHttpHeaderWords(array $headerValues) {
        if (\count($headerValues) === 0) {
            return '';
        }
        // evaluate if its a multidimensional array
        $first = reset($headerValues);
        if (!\is_array($first)) {
            $headerValues = [$headerValues];
        }

        $spaces = '\\s';
        $ctls = '\\x00-\\x1F\\x7F'; //@see http://stackoverflow.com/a/1497928/413531
        $tspecials = '()<>@,;:<>/[\\]?.="\\\\';
        $tokenPattern = "#^[^{$spaces}{$ctls}{$tspecials}]+$#";
        $result = [];
        foreach ($headerValues as $header) {
            $attr = [];
            foreach ($header as $key => $val) {
                if (isset($val)) {
                    if (preg_match($tokenPattern, $val)) {
                        $key .= "=$val";
                    } else {
                        $val = preg_replace('/(["\\\\])/', '\\\\$1', $val);
                        $key .= "=\"$val\"";
                    }
                }
                $attr[] = $key;
            }
            if (\count($attr)) {
                $result[] = implode('; ', $attr);
            }
        }

        return implode(', ', $result);
    }
public static function joinHttpHeaderWords(array $headerValues) {
        if (\count($headerValues) === 0) {
            return '';
        }
        // evaluate if its a multidimensional array
        $first = reset($headerValues);
        if (!\is_array($first)) {
            $headerValues = [$headerValues];
        }

        $spaces = '\\s';
        $ctls = '\\x00-\\x1F\\x7F'; //@see http://stackoverflow.com/a/1497928/413531
        $tspecials = '()<>@,;:<>/[\\]?.="\\\\';
        $tokenPattern = "#^[^{$spaces}{$ctls}{$tspecials}]+$#";
        $result = [];
        foreach ($headerValues as $header) {
            $attr = [];
            foreach ($header as $key => $val) {
                if (isset($val)) {
                    if (preg_match($tokenPattern, $val)) {
                        $key .= "=$val";
                    } else {
                        $val = preg_replace('/(["\\\\])/', '\\\\$1', $val);
                        $key .= "=\"$val\"";
                    }
                }
                $attr[] = $key;
            }
            if (\count($attr)) {
                $result[] = implode('; ', $attr);
            }
        }

        return implode(', ', $result);
    }
HTTP Headers Util 0.1.

@author: Keyvan Minoukadeh – hide@address.com – http://www.keyvan.net

@license GNU General Public License, version 2

@see (source)http://www.phpkode.com/source/s/http-headers-utility/http-headers-utility/HTTP_Headers_Util.php

join_header_words

This will do the opposite of the conversion done by split_header_words().
It takes a list of anonymous arrays as arguments (or a list of
key/value pairs) and produces a single header value.  Attribute values
are quoted if needed.

Example:

join_header_words(array(array("text/plain" => null, "charset" => "iso-8859/1")));
join_header_words(array("text/plain" => null, "charset" => "iso-8859/1"));

will both return the string:

text/plain; charset="iso-8859/1"

@param array $headerValues

@return string

@see http://tools.ietf.org/html/rfc5988#section-5