tags:

views:

4319

answers:

4

I've got two arrays of the same size. I'd like to merge the two so the values of one are the key indexes of the new array, and the values of the new array are the values of the other.

Right now I'm just looping through the arrays and creating the new array manually, but I have a feeling there is a much more elegant way to go about this. I don't see any array functions for this purpose, but maybe I missed something? Is there a simple way to this along these lines?

$mapped_array = mapkeys($array_with_keys, $array_with_values);
+16  A: 

See array_combine().

aib
Wow, I guess it's just one of those mornings - how did I miss this? Perfect, thank you!
Erik
+2  A: 
widgisoft
A: 

Check out this: http://php.net/manual/en/function.array-merge.php

Regards!

A: 

This should do the trick:

function array_merge_keys($ray1, $ray2){ $keys = array_merge(array_keys($ray1),array_keys($ray2)); $vals = array_merge($ray1, $ray2); return array_combine($keys,$vals); }

Mathias