dongsong1911
2013-05-21 09:27
浏览 29
已采纳

在javascript数组中转换Php数组

I have php array like this

[0]->imgae_name=1
 image_url=a

[1]->imgae_name=2
 image_url=b

i want to convert this array in javascript array how can i do this?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douxianji3367 2013-05-21 09:29
    已采纳

    You could convert the PHP array to JSON using json_encode, then echo the json string into the Javascript in your page and use Javascript JSONObject to convert it into a JS array

    打赏 评论
  • doujing3896 2013-05-21 09:28
    json_encode($your_array);
    

    is the right way to do this. It converts your php array to this string:

    [{imgae_name:1,image_url:"a"},{imgae_name:2,image_url:"b"}]
    

    Then you only have to assign it to a variable in a script tag.

    打赏 评论
  • douji4223 2013-05-21 09:31

    In php

    <?
    $array = array("A" => "1", "B" => "2");
    echo json_encode($array);
    ?>
    

    In Javascript

    var mObject = eval("(" + phpecho + ")");
    

    Then you can access the value :

     mObject.A // 1
    
    打赏 评论

相关推荐 更多相似问题