我有一个javascript,它联系一个php页面,该页面从数据库中获取一些数据并将其保存在数组中。
我想获取该数组并使用jquery将其循环出来。
数组如下所示:
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 )
);
答案 0 :(得分:2)
执行此操作的最佳方法是对数组进行json_encode,然后将结果回显给JavaScript / jQuery:
这是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);
?>
这应该给你这样的东西:
[{"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"}]
并使用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 :(得分:0)
您可以随时使用php将该数组打印为js列表。或者通过json导出它。