Possible Duplicate:
convert php array to javascript array
passing PHP objects to javascript
Greetings Stackoverflow
I'm in quite a problem, I want to pass an array of objects from PHP to Javascript for processing. How could I do this. Tnx in advance
-
1you need JSONAlnitak– Alnitak2012年05月20日 16:27:44 +00:00Commented May 20, 2012 at 16:27
-
2did you search Stackoverflow before asking the question? -- look at the "related" list in the side panel; this question has been asked dozens of times already.Spudley– Spudley2012年05月20日 16:29:18 +00:00Commented May 20, 2012 at 16:29
-
yes i had sir.. other posts did not say how to convert php object to javascript objectwawaragna– wawaragna2012年05月20日 17:59:56 +00:00Commented May 20, 2012 at 17:59
2 Answers 2
Often this is done using an Ajax call to your PHP app, and have PHP generate JSON with the data being sent for processing (see json_encode()).
I've also seen PHP programs generate the JavaScript with the data encoded directly in the script, but I would not recommend this approach.
Comments
When echoing the javascript in the view, you can use echo json_encode($array) to convert the PHP array to JSON which can then be inserted into a javascript variable.