tags:

views:

76

answers:

4

Before I write my own function to do it, is there any built-in function, or simple one-liner to convert:

Array
(
    [0] => pg_response_type=D
    [1] => pg_response_code=U51
    [2] => pg_response_description=MERCHANT STATUS
    [3] => pg_trace_number=477DD76B-B608-4318-882A-67C051A636A6
)

Into:

Array
(
    [pg_response_type] => D
    [pg_response_code] =>U51
    [pg_response_description] =>MERCHANT STATUS
    [pg_trace_number] =>477DD76B-B608-4318-882A-67C051A636A6
)

Just trying to avoid reinventing the wheel. I can always loop through it and use explode.

A: 

Edit - didn't read the question right at all, whoops..

A foreach through the array is the quickest way to do this, e.g.

foreach($arr as $key=>$val)
{
    $new_vals = explode("=", $val);
    $new_arr[$new_vals[0]] = $new_vals[1];
}
ConroyP
A: 

This should be around five lines of code. Been a while since I've done PHP but here's some pseudocode

foreach element in the array
explode result on the equals sign, set limit = 2
assign that key/value pair into a new array.

Of course, this breaks on keys that have more than one equals sign, so it's up to you whether you want to allow keys to have equals signs in them.

belgariontheking
+3  A: 

I can always loop through it and use explode.

that's what you should do.

SilentGhost
You definitely should go with a loop here. Cryptic one-liners are ugly pieces of code.
Philippe Gerber
right, there's no function like this in php's built-in. only loop.
Jet
A: 

You could do it like this:

$foo = array(
    'pg_response_type=D',
    'pg_response_code=U51',
    'pg_response_description=MERCHANT STATUS',
    'pg_trace_number=477DD76B-B608-4318-882A-67C051A636A6',
);

parse_str(implode('&', $foo), $foo);

var_dump($foo);

Just be sure to encapsulate this code in a function whose name conveys the intent.

Ionuț G. Stan
and values shouldn't contain ampersand of course.
SilentGhost