views:

71

answers:

4

Question asks itself ^

I've heard I can mimic this using http_build_query, but I'd rather use a function that's meant for this.

Thanks for replies


input example:

$assoc = array(
    "fruit" => "banana",
    "berry" => "blurberry",
    "vegetable" => "lettice"
);

output wanted: (i get this with http_build_query)

string(46) "fruit=banana,berry=blurberry,vegetable=lettice"

output from reversal wanted is the same as input - that's my current problem

A: 

Found a function in the php.net comments for implode:

function implode_with_key($glue = null, $pieces, $hifen = ',') {
    $return = null;
    foreach ($pieces as $tk => $tv) $return .= $glue.$tk.$hifen.$tv;
    return substr($return,1);
}

Credit to memandeemail at gmail dot com

Stephen
+1  A: 

Would serialize() meet your needs? Depends on what you'd be doing with the array.

Edit: Never mind, just spotted your clarification. Read on.

ASpencer
+2  A: 

Implode with

serialize($array);

Explode with

unserialize($array);
Feeloow
Output doesn't match what I'd prefer, but otherwise it's perfect and I ended up using this.
Codemonkey
+3  A: 

try json_encode

stereofrog