duanduan8439 2013-08-13 15:05
浏览 54
已采纳

php多维数组到js

I know this question is a duplicate but I really would like some help with this. I know the only way to send a php array to js is by encoding with json. Well I tried that but nothing works. Here's my code:

<?php

Header("content-type: application/x-javascript");
error_reporting(E_ERROR | E_WARNING | E_PARSE);

    $months = Array();

    $months = ['January'=>array(), 'February'=>array(), 'March'=>array(), 'April'=>array(), 'May'=>array(), 'June'=>array(), 'July'=>array(), 'August'=>array(), 'September'=>array(), 
                        'October'=>array(), 'November'=>array(), 'December'=>array() ]; 



    // Connect to MySQL
         if ( !( $database = mysql_connect( "localhost",
            "root", "" ) ) )                      
            die( "Could not connect to database </body></html>" );

    // open Events database
         if ( !mysql_select_db( "Events", $database ) )
            die( "Could not open Events database </body></html>" );

            foreach($months as $month => $arr) {

            $result = mysql_query("SELECT * FROM posted_events WHERE Month_ = '$month' ") 
                    or die ('Error updating database because: '.mysql_error());

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $months[$month][] =  $row['DayNum']; 

    }
}

            echo "var months = jQuery.parseJSON('$months');";
<?php

So the JS array variable 'months' should contain all the elements that was in the PHP's array variable right? Well it doesn't work. I don't know what's wrong. Help please? I need to use this array in my external JS file to do some more work with my program, so it's really important. Thanks guys.

  • 写回答

1条回答 默认 最新

  • dongze8698 2013-08-13 15:09
    关注

    You'll need to encode the JSON data using PHP. This can be done using json_encode(). Once it's encoded, you can pass the variable to Javascript, like so:

    $months = json_encode($months);
    echo "var months = $months ;";
    

    Also, you're currently using jQuery.parseJSON. But, the jQuery documentation says:

    The JSON standard does not permit "control characters" such as a tab or newline. An example like $.parseJSON('{"testing":"1\t2 3"}') will throw an error in most implementations because the JavaScript parser converts the string's tab and newline escapes into literal tab and newline; doubling the backslashes like 1\\t2\ 3 yields expected results. This problem is often seen when injecting JSON into a JavaScript file from a server-side language such as PHP.

    Use JSON.parse() instead.

    json = JSON.parse(months);
    for(var i in json) {
    
    //code
    
    }
    

    Most browsers support this, but for those that doesn't, you can use json2.js.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理