什么是在PHP中合并2个数组的最快方法
问题描述:
I have 2 arrays:
1st array {0=> "google", 1=> "apple", 2=> "microsoft"}
2nd array {0=> "awesome", 1=> "sucks", 2=> "oh man!"}
now what I want is to merge the 2 arrays in this form:
array {"google"=>"awesome", "apple"=>"sucks", "microsoft"="oh man!"}
whats the most efficient way to do this? thanks
答
Use 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);
答
Have you tried with array_combine..??try like this
$array3 = array_combine($array1, $array2);
print_r($array3);
you will get like
array {"google"=>"awesome", "apple"=>"sucks", "microsoft"="oh man!"}
and you can also try with "array_merge" like
$array3 = array_merge($array1, $array2);
print_r($array3);