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 ~