合併2個陣列的最快方法我有2列:什麼在PHP
1st array {0=> "google", 1=> "apple", 2=> "microsoft"}
2nd array {0=> "awesome", 1=> "sucks", 2=> "oh man!"}
現在我想的是這種形式合併2個陣列:
array {"google"=>"awesome", "apple"=>"sucks", "microsoft"="oh man!"}
什麼最有效如何做到這一點? 感謝
合併2個陣列的最快方法我有2列:什麼在PHP
1st array {0=> "google", 1=> "apple", 2=> "microsoft"}
2nd array {0=> "awesome", 1=> "sucks", 2=> "oh man!"}
現在我想的是這種形式合併2個陣列:
array {"google"=>"awesome", "apple"=>"sucks", "microsoft"="oh man!"}
什麼最有效如何做到這一點? 感謝
使用array_combine — Creates an array by using one array for keys and another for its values
$a = array(0=> "google", 1=> "apple", 2=> "microsoft");
$b = array(0=> "awesome", 1=> "sucks", 2=> "oh man!");
$c = array_combine($a, $b);
print_r($c);
感謝的快速解決方案! – SolidSnake
@RobinVanPersi如果解決了您的問題,請接受解決方案。 –
你有沒有試着用array_combine ??嘗試這樣
$array3 = array_combine($array1, $array2);
print_r($array3);
,你會得到像
array {"google"=>"awesome", "apple"=>"sucks", "microsoft"="oh man!"}
,你可以也可以試試「array_merge「像
$array3 = array_merge($array1, $array2);
print_r($array3);
看起來更像是紅寶石哈希比PHP陣列;) – swapnesh