0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't notice the output format you wanted. Construct your array the way you want it then json_encode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.