-1

How can I convert this to an array in PHP?

&height=0&weight=2&width=10

I'm passing a data from a jquery function using .serialize() to a PHP function.

Any ideas?

Greg
  • 9,068
  • 6
  • 49
  • 91
iamhealed
  • 35
  • 6

2 Answers2

5

Can be done within one line. :)

parse_str('&height=0&weight=2&width=10', $array);

print_r($array);
Terry Harvey
  • 840
  • 6
  • 12
  • 1
    I don't know why down voted, I just up-voted against it :) –  Jul 13 '13 at 00:21
  • Yup that is correct +1 – Khawer Zeshan Jul 13 '13 at 00:22
  • 1
    One-liner only if you don't have _strict error reporting_! :] If you have, you'll have to declare `$array = array()` first or you get stuck with undeclared variable notice. And one-liner dream is gone! :] – trejder Sep 24 '13 at 13:38
2

Depending on what type of request you are performing, it may already be in an array. Have a look at the PHP documentation on $_GET and $_POST global variables.

To view the contents of said array. You can use the function print_r() which will show you the contents of the array.

print_r($_GET)

print_r($_POST)

Access individual items in the array by the item's key. For example:

echo $_POST['height'];

Ryan
  • 14,392
  • 8
  • 62
  • 102