這裏是你可以使用函數:
function rewrap(Array $input){
$key_names = array_shift($input);
$output = Array();
foreach($input as $index => $inner_array){
$output[] = array_combine($key_names,$inner_array);
}
return $output;
}
這裏是一個演示:
// Include the function from above here
$start = array(
0 => array("name", "address", "city"),
1 => array("anoop", "palasis", "Indore"),
2 => array("ravinder", "annapurna", "Indore")
);
print_r(rewrap($start));
此輸出:
Array
(
[0] => Array
(
[name] => anoop
[address] => palasis
[city] => Indore
)
[1] => Array
(
[name] => ravinder
[address] => annapurna
[city] => Indore
)
)
注:您的第一個數組定義索引1
兩次,所以我改變了第二個o ne到2
,像這樣:
array(0 => array("name", "address", "city"), 1 => array("anoop", "palasis", "Indore"),2 => array("ravinder", "annapurna", "Indore"))
這可能只是一個錯字。
在你的問題請註明(如果)你想爲關鍵字的第一個元素使用的項目。 – 2012-02-18 18:07:09
我想將該數組放入鍵值對中...請幫助我。 – 2012-02-18 18:08:43