未排序的数组,headers_list(),为数组创建键? [英] Unsorted array, headers_list(), make keys for array?
本文介绍了未排序的数组,headers_list(),为数组创建键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如您所知, headers_list()
-PHP函数返回如下数组:
As you know, the headers_list()
-PHP-function returns an array like this:
Array
(
[0] => Content-type: text/html; charset=utf-8
[1] => key: value
[2] => key2: value2
)
或在此示例中),如何使用arrays值中的键构建新数组?例如,上面的数组变为:
In general (or with this example), how can you build a new array using the "key" in the arrays value? For example, the array above becomes:
Array
(
['Content-type'] => text/html; charset=utf-8
['key'] => value
['key2'] => value2
)
这可能真的很基本,但我仍然是初学者:)
This is probably really basic, but I am still a beginner :)
推荐答案
这应该起作用:
$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
并不是很重要,但是对于价值而言却确实如此,因为您将删除':'之后的空格。
Probably trim for $key
is not really important, but for value it really is, because you will remove the space after the ':'.
这篇关于未排序的数组,headers_list(),为数组创建键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文