简体   繁体   English

json编码的php数组在javascript中返回undefined

[英]json encoded php array returns undefined in javascript

This is a follow up to a previous post. 这是前一篇文章的后续内容

I'm attempting to pass some data to javascript via json_encode. 我试图通过json_encode将一些数据传递给javascript。

I thought I would be able to put the data in to a javascript array as follows: 我以为我可以将数据放入javascript数组中,如下所示:

    var data = [<?php echo json_encode($result_array); ?>];

But when I try to call values they return as undefined. 但是当我尝试调用值时,它们返回为undefined。 I imagine that it might be putting the data as a singular string inside the array. 我想它可能是将数据作为单个字符串放在数组中。

If anyone can provide advice that would be great. 如果有人能提供很棒的建议。 Thanks a lot! 非常感谢!

如果$result_array是php中的数组,那么你只需要:

var data = <?php echo json_encode($result_array); ?>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM