Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I save php variable into a 2D JavaScript array.
inside php file :

for ($i=0;$i<$max;$i++) {
 $needHelp1[$i];
 $needHelp2[$i];
 }

I need to save the $needHelp1[$i] and $needHelp2[$i]to java script 2Darray called var needHelp = [] which is inside the same php file.

[ $needHelp1[0] , $needHelp2[1] 
  $needHelp1[1] , $needHelp2[1]
  $needHelp1[2] , $needHelp2[1] 
  $needHelp1[3] , $needHelp2[1] ]

I want the javascript array to be something like this.

Thanks.

share|improve this question

1 Answer

json_encode is what your looking for,

var needHelp = <?php echo json_encode($needHelp1); ?>

When you run your php check the source and you should have a javascript array.

share|improve this answer
I didn't notice the output format you wanted. Construct your array the way you want it then json_encode. – shapeshifter Feb 13 at 4:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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