我的PHP响应看起来像这个多维数组:
{
"results":
{"id":"153","title":"xyz","description":"abc"},
{"id":"154","title":"xyy","description":"abb"},
"filter_color":{"Red":1,"Blue":8},
"count_rows":{"rows":"10"}
}
使用jquery我想获取此数据并在表格中显示数据......但是如何选择特定的键/值对? (例如,我只想显示结果中的所有描述)。
答案 0 :(得分:2)
如果你的PHP数组是这样的:
$myArray = array ( 'results' => array (
array ( 'id' => '153',
'title' => 'xyz',
'description' => 'abc' ),
array ( 'id' => '154',
'title' => 'xyy',
'description' => 'abb' )
),
'filter_color' => array ( 'Red' => 1, 'Blue' => 8 ),
'count_rows' => array ( 'rows' => '10' )
);
使用json_encode()
:
{
"results":
[ {"id":"153","title":"xyz","description":"abc"},
{"id":"154","title":"xyy","description":"abb"} ],
"filter_color":{"Red":1,"Blue":8},
"count_rows":{"rows":"10"}
}
你的jQuery看起来有点像这样:
$.ajax({
url: "http://example.com",
success: function(data) {
// Then you can do this to print out the description of each result
// in the browser console... or append the info to a table
for(var i in data.results) {
console.log(data.results[i].description);
$("table").append("<tr><td>" + data.results[i].title + "</td><td>" + data.results[i].description + "</td></tr>");
}
}
});