drkxgs9358 2009-04-04 10:09
浏览 269

通过ajax将多个数组传递给javascript

To pass an array from PHP to javascript via ajax, I am going to use json_encode, and recieve it on the javascript end. However, I will be returning multiple rows from my MySQL database, and need to return multiple JSON-formatted arrays. One way I thought of doing this was to string the JSON arrays together in php with some obscure character, such as a pipe character, and then separate them on the javascript end. But is there a more elegant way to do this?

Edit: this post explains what I am trying to do.

  • 写回答

1条回答 默认 最新

  • dongmisa4779 2009-04-04 10:13
    关注

    Just send them as an JSON-encoded array of arrays.

    <?php
    $row=array('foo'=>'bar','baz'=>'quux');
    echo json_encode(array($row,$row,$row,$row));
    ?>
    

    Results in

    [
     {"foo":"bar","baz":"quux"},
     {"foo":"bar","baz":"quux"},
     {"foo":"bar","baz":"quux"},
     {"foo":"bar","baz":"quux"}
    ]
    

    This can then be processed exactly like an array on the client side.

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题