-5

I need to pass a php variable to a jquery function. the php variable is an array of unknown size. is there a way to pass each member of the array to be processed individually by the function?

Forsyth
  • 1
  • 1

2 Answers2

0

I Guess you should use JSON to pass the php variable to jquery. its the easiest way to do that.

PaxBin
  • 111
  • 3
  • 14
0

You can use JSON to print the array and parse that to receive a JS object.

I assume you use AJAX (directly printing into the javascript source is possible but ill-advised).

Your php script needs to print output directly:

print_r(json_encode($data_array));

And on the receiving end use $.parseJSON:

var data_array = $.parseJSON(ajax_result);
Maxim Kumpan
  • 2,545
  • 2
  • 19
  • 23
  • `echo` instead of `print_r` for passing it to a JS var/Ajax response btw. – Fabrício Matté Jun 11 '13 at 08:05
  • @FabrícioMatté There is little difference in this case. print_r does not format the data in any way unless it is an object or an array. Also, you can rig print_r to return the value and use it in expressions, which is impossible for echo. – Maxim Kumpan Jun 11 '13 at 08:12