0

Im running a javascript code which reads values from different XML files and it generates a multidimentional array based on those values. Now I need to pass this array to a PHP page. I tried different but it always pass the arrray as string not as an array. Anyone has an idea :( ... and thank you very much

2 Answers 2

1

What Caleb said. Use this and JSON encode your JS array to a string, send it over to PHP and use json_decode to decode it into a PHP array.

Sign up to request clarification or add additional context in comments.

Comments

0

You need a JSON encoder/decoder to do that. Prototype has it implemented by default and with jQuery you can use jQuery-JSON

For example if you use Prototype as your JS library then you can convert your array into a string like that:

var example_multi_dim_arr = {"a":[1,2,3], "b": [4,5,6]};
var string_to_be_sent_to_server = Object.toJSON(example_multi_dim_arr);

And in the PHP side (assuming that the JSON string is passed to the script as a POST variable)

$multi_dim_arr = json_decode($_POST["variable_with_json"], true);

The last true field in json_decode indicates that the output should be in the form of an array ($multi_dim_arr["a"]) and not as an object ($multi_dim_arr->a).

NB! the function json_decode is not natively available in PHP 4, you should find a corresponding JSON class if you are using older versions of PHP. In PHP 5 everything should work fine.

5 Comments

Hi, thank you for the answer ;) .. im using this library and it looks fine for coding and decoding since im not getting any error message. Plus im using 'GET' method to pass this array from java script to php. The problem is that im not getting any value when im trying echo command ... the code in the php page is: $multi_dim_arr= $_REQUEST['the_array']; $clustering_activities = json_decode($multi_dim_arr); echo $clustering_activities[0][0]; and im not getting any value. it seems that the decoding is not working in a proper way. Any idea !? .. and thanks ;)
just forget to mention that i also tried it with true value like $multi_dim_arr = json_decode($_POST["variable_with_json"], true); but the same... always no values :(((
do you encode the json string in the GET url? url="example.com/…
smth. went wrong, it should have been (withou the extra spaces) url="http : / / example . com /target.php?the_array="+encodeURICompontent(json_str)
in the javascript side, im doing the coding in this way var string_to_be_sent_to_server = Object.toJSON(clustering_activities); where clustering_activities is my array which i want to send. then i open a php page var ss = window.open("activity_creater.php?the_array="+ string_to_be_sent_to_server + ..). i can see the array in up in address bar in array format which means that the coding going well!! and in the php side im using the code i posted in my first cemmont.im not getting any wrong message but just no values when im trying to print a value from the array. im getting creay :)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.