dongyan6235 2012-01-17 15:39
浏览 28
已采纳

PHP数组回显我可以访问的javascript数组?

I have a php array called $user.

At the minute I am getting the variables across to javascript by manually adding them like so.

var username = ' . $user['username'] . ';

And so on is there a way I can make php echo a javascript array that I can access?

  • 写回答

2条回答 默认 最新

  • dongzhao5834 2012-01-17 15:41
    关注

    You're looking for json_encode

    PHP:

    $json_user = json_encode($user);
    

    JavaScript:

    var user = JSON.parse('<?php echo $json_user; ?>');
    

    That's untested code but the idea behind it is sound

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?