-2

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

Community
  • 1
  • 1
wawaragna
  • 37
  • 2
  • 10

2 Answers2

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.

acrosman
  • 12,814
  • 10
  • 39
  • 55
2

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.

Ian Gregory
  • 5,770
  • 1
  • 29
  • 42
  • ... on recent versions of PHP. – Alnitak May 20 '12 at 16:28
  • ah, if only my web host had that... :( – Alnitak May 20 '12 at 16:31
  • 1
    @Alnitak Ah, another web host in the dark ages. There's a pure PHP version too https://github.com/stetsenko/code/blob/master/json_encode.php – Ian Gregory May 20 '12 at 16:32
  • But sir, how do I maintain the propoerties of the objects I have passed using json_encode? I have tried doing json_enode but the new array cannot access the properties of the object i just passed.. – wawaragna May 20 '12 at 17:45