2010-12-17 69 views
1

大家知道,在headers_list() -PHP-函數返回一個這樣的數組:Unsorted array,headers_list(),爲數組創建鍵?

Array 
(
    [0] => Content-type: text/html; charset=utf-8 
    [1] => key: value 
    [2] => key2: value2 
) 

通常(或者與該實施例中),則如何可以建立利用在陣列值的「鑰匙」一個新的數組?例如,陣列上方變爲:

Array 
(
    ['Content-type'] => text/html; charset=utf-8 
    ['key'] => value 
    ['key2'] => value2 
) 

這可能是很基本的,但我還是個初學者:)

回答

1

這應該工作:

$headers = header_list(); 
foreach($headers as $header){ 
    // split each header by ':' and assign them to $key and $value 
    list($key, $value) = explode(':', $header, 2); // limit the explode to 2 items. 
    // add trimed variables to the new array 
    $new_headers[trim($key)] = trim($value); 
} 

大概爲修剪是$key不非常重要,但實際上它的價值在於,因爲你會在':'之後移除空格。

+0

你打我,就像2秒! – JakeParis 2010-12-17 22:43:17

+1

最重要的不是速度,而是質量:) – s3v3n 2010-12-17 22:44:23

+2

將調用保存到trim()並以「:」爆炸會更快。略。 – JakeParis 2010-12-17 22:50:10

1
foreach($headers as $v) { 
    list($name,$val) = explode(': ',$v); 
    $newArray[$name] = $val; 
} 

// $newArray is your requested array. 
1
$headers = header_list(); 
$keyed_headers = array(); 
foreach ($headers as $header) { 
    $break = explode(': ', $header, 2); 
    $keyed_headers[$break[0]] = $break[1]; 
} 

UPDATE

一時心血來潮,我決定,這將是MoarAwesome TM使用一個類來解決這個問題。這是結果:

// definition 
class HeaderArray extends ArrayObject { 
    static function init() { 
     return new self(header_list()); 
    } 
    public function get_assoc() { 
     $result = new self(); 
     foreach ($this as $v) { 
      $b = explode(': ', $v, 2); 
      $result[$b[0]] = $b[1]; 
     } 
     return $result; 
    } 
} 

// implementation 
$headers = HeaderArray::init()->get_assoc(); 
2

你應該使用這個功能apache_response_headers取而代之的,是返回數組中的頭......

然而,函數名揭露它的工作原理爲Apache只

更新: PHP需要使用--with-apxs2

+1

+1很酷的一個。如果OP使用apache。 (可能) – Stephen 2010-12-17 22:49:54

+0

哦,boi ...你是對的... – ajreal 2010-12-17 22:51:13

+0

我有一個Apache服務器,但我不確定我的客戶端是否有這個,所以我會接受其他答案,排序標題字段,因爲這就是我所要求的,但是我最終可能會最終使用你的解決方案:)(我討厭在按下Enter鍵時發佈評論) – Emil 2010-12-17 22:58:49

1

編譯以下函數能夠處理多個具有相同字段名稱的頭字段並將它們組合起來as stipulated by the specification

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma.

function getHeaderFields() { 
    $fields = array(); 
    $index = array(); 
    foreach (headers_list() as $field) { 
     list($name, $value) = explode(':', $field, 2); 
     $name = trim($name); 
     if (isset($index[strtolower($name)])) { 
      $name = $index[strtolower($name)]; 
     } else { 
      $index[strtolower($name)] = $name; 
     } 
     if (isset($fields[$name])) { 
      $fields[$name] .= ', '.trim($value); 
     } else { 
      $fields[$name] = trim($value); 
     } 
    } 
    return $fields; 
} 

這個工作在相反apache_response_headers或其他提到的解決方案:

setcookie('foo', 'bar'); 
setcookie('bar', 'quux'); 
$headers = getHeaderFields(); 
var_dump($headers['Set-Cookie']);  // string(17) "foo=bar, bar=quux" 
$headers = apache_response_headers(); 
var_dump($headers['Set-Cookie']);  // string(8) "bar=quux" 
1

我覺得這是更好的:你應該reimplode的情況下出現了剩餘一個「:」在一個價值

$hList = headers_list(); 

    foreach ($hList as $header) { 
     $header = explode(":", $header); 
     $headers[ array_shift($header) ] = trim(implode(":", $header)); 
    }