Passing string arrays to php in js

Source: Internet
Author: User
Why does js convert the string array passed to php from js to a string array in php? the code passed by ajax is as follows:
Js end:
Var selected = new Array ('1', '2 ');
JSON. stringify (selected );
Post ....

Php:
$ Selected = json_decode ($ _ POST ['selected']);
In this way, $ selected is empty.
$ Selected = $ _ POST ['selected'];
In this case, $ selected is a string and I want an array.

It's been a long time to ask for help...


Reply to discussion (solution)

Var_dump ($ selected); what is the result?

Echo $ _ POST ['selected'];
Paste the result!

Echo $ _ POST ['selected'];
Paste the result!

The value is 1, 2, and the count ($ selected) value is 1.

Complete the print_r ($ _ POST ['selected']) result.

If $ selected = array (1, 2 );
Not count ($ selected) = 1
It should be 2

$ A = explode (',', $ _ POST ['selected']);
Print_r ($ );

$ A = explode (',', $ _ POST ['selected']);
Print_r ($ );
I don't really want to use this method. what I really want to use is a string array. it may be a problem to split it with symbols. I want to use json, but I don't know much about it.

Complete the print_r ($ _ POST ['selected']) result.

If $ selected = array (1, 2 );
Not count ($ selected) = 1
It should be 2

It is indeed 2, and json is useless. Please help ~

Var_dump ($ selected); what is the result?

Var_dump ($ selected); what is the result?

That is, 1 and 2 are equivalent to a string like "1 and 2 ".

JSON. stringify (an array parameter) is a string after processing?

JSON. stringify (an array parameter) is a string after processing?
I don't know much about it. I tried it. it's useless.

Var selected = new Array ('1', '2 ');
Selected = JSON. stringify (selected );
Then pass the selected

Solved. thank you ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.