做到這一點,最好的辦法是json_encode數組,然後呼應結果出來的JavaScript/jQuery的:
json_encode PHP function
這裏是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);
我會簡單地轉換數組到JSON和發送JSON字符串到客戶端。我認爲jQuery可以在沒有擴展名的情況下反序列化JSON。 – Wukerplank