-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?

9
  • 1
    what about api.jquery.com/serializeArray Commented Jul 13, 2013 at 0:16
  • @Akam: He's asking how to do it unserialize that string into an array in PHP Commented Jul 13, 2013 at 0:16
  • yes, then see php.net/manual/en/function.parse-str.php Commented Jul 13, 2013 at 0:18
  • 1
    If you're using serialize() properly, it would already be an array in either $_POST or $_GET on the serverside, depending on the method you're using. Commented Jul 13, 2013 at 0:19
  • 1
    It is already an array. print_r($_GET), print_r($_POST) Commented Jul 13, 2013 at 0:21

2 Answers 2

5

Can be done within one line. :)

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

print_r($array);
Sign up to request clarification or add additional context in comments.

3 Comments

I don't know why down voted, I just up-voted against it :)
Yup that is correct +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! :]
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'];

3 Comments

As i understand the question, this is the answer
@roasted I understood the question as you did.
thanks! it is already an array..hehe

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.