Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by deceze, MaVRoSCy, Mohammad Adil, HAL9000, Felix Kling Jun 11 '13 at 8:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

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);
share|improve this answer
    
echo instead of print_r for passing it to a JS var/Ajax response btw. –  Fabrício Matté Jun 11 '13 at 8: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 8:12

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

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.