doushai4890 2014-07-03 07:39
浏览 92
已采纳

使用array_push将元素添加到数组中

I have an array & I'd like to add data to the array using PHP. I can't add it directly.

How would I do this using array_push?

<script type="text/javascript"> 
    var parks = [{"title":"Football Park","lat":"55.86234","lng":"-4.250635999999986","img":"icon.png"}]
</script>
  • 写回答

2条回答 默认 最新

  • dongliang1873 2014-07-03 07:44
    关注

    You can try:

    //if your JSON string on server side <--PHP-->
        $park = json_decode([{"title":"Football Park","lat":"55.86234","lng":"-4.250635999999986","img":"icon.png"}],true);
        $park['key'] = 'someValue';
        $newJSON = json_encode($park);
    
    // if your JSON string on client side <--JS-->
    var parkObj = JSON.parse(park);
    parkObj.key = 'someValue';
    console.log(JSON.stringify(parkObj));
    //[{"title":"Football Park","lat":"55.86234","lng":"-4.250635999999986","img":"icon.png","key" : "someValue"}]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?