dongmei9203 2011-03-08 13:54
浏览 31
已采纳

数组从php到jquery通过ajax

I have a javascript that contacts a php page which gets some data from a database and save it in an array.

I want to take that array and loop it out with jquery.

The array looks like this:

Array ( 
  [0] => Array ( [image] => article_list1.png [title] => Everyone involved in OMS in Ghent ) 
  [1] => Array ( [image] => article_list1.png [title] => Everyone involved in OMS in Ghent ) 
  [2] => Array ( [image] => article_list1.png [title] => Everyone involved in OMS in Ghent ) 
  [3] => Array ( [image] => article_list1.png [title] => Everyone involved in OMS in Ghent ) 
  [4] => Array ( [image] => article_list1.png [title] => Everyone involved in OMS in Ghent ) 
  [5] => Array ( [image] => article_list1.png [title] => Everyone involved in OMS in Ghent ) 
);
  • 写回答

2条回答 默认 最新

  • drwo2014 2011-03-08 13:58
    关注

    The best way to do this is to json_encode the array and then echo the result out to the JavaScript/jQuery:

    json_encode PHP function

    Here is the PHP php demo

    <?php
    $myarray = Array ( 
      Array ( 'image' => 'article_list1.png', 'title' => 'Everyone involved in OMS in Ghent' ), 
      Array ( 'image' => 'article_list1.png', 'title' => 'Everyone involved in OMS in Ghent' ), 
      Array ( 'image' => 'article_list1.png', 'title' => 'Everyone involved in OMS in Ghent' ), 
      Array ( 'image' => 'article_list1.png', 'title' => 'Everyone involved in OMS in Ghent' ), 
      Array ( 'image' => 'article_list1.png', 'title' => 'Everyone involved in OMS in Ghent' ), 
      Array ( 'image' => 'article_list1.png', 'title' => 'Everyone involved in OMS in Ghent' ) 
    );
    
    echo json_encode($myarray);
    ?>
    

    This should give you something like this:

    [{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"}]
    

    and to access it using jQuery jsfiddle demo:

    var myJson = '[{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"},{"image":"article_list1.png","title":"Everyone involved in OMS in Ghent"}]';
    myJson = JSON.parse(myJson);
    for(var i=0; i<myJson.length; i++)
        console.log(myJson[i].image+ ' ' + myJson[i].title);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分