什么是在PHP中合并2个数组的最快方法

时间:2013-04-01 05:26:56

标签: php arrays merge

我有2个阵列:

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 个答案:

答案 0 :(得分:3)

使用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);

答案 1 :(得分:0)

你试过 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);