tags:

views:

230

answers:

3

Possible Duplicate:
Elegant way to merge two arrays as key value pairs in PHP?

I have two arrays:

Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
    [4] => E
)

Array
(
    [0] => APPLE
    [1] => BANANNA
    [2] => CRANBERRY
    [3] => DURIAN
    [4] => EGGPLANT
)

I want to combine them so that first value of the first array will be the the first key of the second array.

Array
(
    [A] => APPLE
    [B] => BANANNA
    [C] => CRANBERRY
    [D] => DURIAN
    [E] => EGGPLANT
)
+3  A: 

Could do it with array_combine:

$new=array_combine($a, $b);

or roll your own:

$new=array();
foreach($a as $idxA=>$valA)
{
   $new[$valA]=$b[$idxA];
}
Paul Dixon
+1  A: 
$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);
Matthew Scharley