views:

69

answers:

1

Is there a quick/easy PHP function to take the VALUES of an array and create an new associative array from the value mappings? or is this a classic iterate, parse, and add?

Example, source array in var_dump() form:

array(3) { 
    [0]=> string(36) "md5=397f7a7501dfe5f18c1057885d698d5d" 
    [1]=> string(7) "foo=bar" 
    [2]=> string(7) "t=18351" 
}

Should be transposed to:

array(3) {
    ["md5"]=> string(32) "397f7a7501dfe5f18c1057885d698d5d"
    ["foo"]=> string(3) "bar" 
    ["t"]=> string(5) "18351"
}
+2  A: 

Try this:

$myArray = array(); // Fill with values in your example
$string_to_parse = implode('&', $myArray);
parse_str($string_to_parse, $result);
var_dump($result);

If your array gets much more complex, with duplicate key/value pairs or other situations, this solution might not work.

Mike B
Ahh, good one, didn't think about using what is generally done on a URL parm line. Let me give it a try.
Xepoch
+1 clever and succinct
alex
Yep, that works, and I don't think I'll have key dups.
Xepoch