可能重复:
Elegant way to merge two arrays as key value pairs in PHP?
我有两个数组:
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
)
Array
(
[0] => APPLE
[1] => BANANNA
[2] => CRANBERRY
[3] => DURIAN
[4] => EGGPLANT
)
我想组合它们,以便第一个数组的第一个值成为第二个数组的第一个键。
Array
(
[A] => APPLE
[B] => BANANNA
[C] => CRANBERRY
[D] => DURIAN
[E] => EGGPLANT
)
答案 0 :(得分:3)
可以使用array_combine:
$new=array_combine($a, $b);
或自己动手:
$new=array();
foreach($a as $idxA=>$valA)
{
$new[$valA]=$b[$idxA];
}
答案 1 :(得分:1)
$array1 = Array
(
[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
);
$array2 = Array
(
[0] => APPLE
[1] => BANANNA
[2] => CRANBERRY
[3] => DURIAN
[4] => EGGPLANT
);
$answer = array_combine($array1, $array2);
print_r($answer);