0

我的 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,我想获取这些数据并在表中显示数据......但是我如何选择特定的键/值对?(例如,我只想显示结果中的所有描述)。

4

1 回答 1

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>");
        }
    }
});
于 2013-06-04T16:17:59.403 回答