-2

Possible Duplicate:
Convert a string into list of arrays

I have a string code=AA&price=10&user_id=5.initially i exploded first with & and then by = but i getting.

array(
      [0] =>code
      [0] =>AA
      [0] =>price
      [0] =>10
      [0] =>user_id
      [0] =>5

 )

My aim is to show

array(
       [code]     => AA
       [price]    => 10
       [user_id]  => 5

)
Community
  • 1
  • 1
nickle
  • 4,636
  • 1
  • 13
  • 11
  • Multiple keys with the same value? Are you sure? Ever heard of [`parse_str()`](http://www.php.net/parse_str)? – Ja͢ck Dec 07 '12 at 13:01

3 Answers3

5
parse_str($str, $values);
var_dump($values);

You are parsing a URL encoded format, use the already existing parse_str to parse it.

deceze
  • 510,633
  • 85
  • 743
  • 889
4
$string = 'code=AA&price=10&user_id=5';
$params = array();
parse_str($string, $params);
print_r($params);
Dale
  • 10,384
  • 21
  • 34
1

http://php.net/manual/en/function.parse-str.php

parse_str — Parses the string into variables

parse_str('code=AA&price=10&user_id=5', $outputArray);
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62