passing array from php to javascript

前端 未结 3 1652
再見小時候
再見小時候 2021-01-25 02:18

Hello I\'m trying to pass several arrays from php to javascript. For some of them it works, for others not. I get an array of filenames and an array which contains the content o

相关标签:
3条回答
  • 2021-01-25 02:30

    Some general advice:

    1. It's difficult to troubleshoot this kind of problem without the error message. If no error is being printed where you can see it, look for files named php.log, error.log, or httpd.log or ask your server admin(s).
    2. Try using print_r() on your arrays to see if there's any difference in how they're structured. For example, just after setting the arrays in PHP:

      print_r($fileArray);
      print_r($listTextArray);
      print_r($falseArray);
      
    3. Rather than constructing the JS arrays via loops, try using the built-in json_encode() function instead. This both simplifies your PHP code and may cause more useful error messages when there are problems:

      var filesArray=<?php json_encode($fileArray) ?>;
      var falseArray=<?php json_encode($falseArray) ?>;
      var textListArray=<?php json_encode($listTextArray) ?>;
      
    0 讨论(0)
  • 2021-01-25 02:34

    you initiate

    var textListArray=new Array(5);
    

    but try to use

    listTextArray
    

    Use the same name and everything will be alright

    0 讨论(0)
  • 2021-01-25 02:38

    The problem is already solved for you. Use json_encode instead.

    print('filesArray = '.json_encode($filesArray));
    

    Note that json_encode demands that your data is utf8 encoded. But you should do that already anyway.

    0 讨论(0)
提交回复
热议问题