doufuhao8085 2012-08-20 12:17
浏览 37
已采纳

使用jquery访问php数组的索引对象

I have a php array of objects istantiated from the following class:

class my_class {
    public $id;
    public $timestamp;
}

These are stored in an array with indexes 0,1,2.....

I am getting this array with jquery through the Session variable and I attempt to print it in the following ways:

var myclass_map = "<?php $_SESSION['myclass_map']?>";
$.each(myclass_map, function(key, value) {
    console.log(key + ' ' + value["id"]);
});

and

var track_map = "<?php $_SESSION['myclass_map']?>";
$.each(myclass_map, function(key, value) {
    console.log(key + ' ' + value.id);
});

but both give me an undefined reference to value while the print out the key. How can I access the objects variables?

  • 写回答

1条回答 默认 最新

  • doujiang1913 2012-08-20 12:22
    关注

    At the very begining of your JS script, print the array converted to js format, using json_encode.

    var track_map = <?=json_encode($map)?>;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?