我从 php 返回一个数组到 json
这里是 php 数组
$cities = array();
while($row = mysql_fetch_array($result)){
$cityRow= array('cityNo'=>$row['city_no'], 'cityName'=>$row['city_name']);
$cities[]=$cityRow;
}
echo json_encode($cities);
这是json
$.getJSON("controllers/Customer.controller.php",param,function(result){
// what should I write here to reach the array elements??
});
最佳答案
您可以使用 .each
遍历对象:
$.getJSON("controllers/Customer.controller.php", param, function(json){
// loop over each object in the array
// 'i' is the index of the object within the array
// 'val' (or this) is the actual object at that offset
$.each(json, function(i, val) {
console.log(val.cityNo); // same as this.cityNo
console.log(val.cityName); // same as this.cityName
});
});
关于php - 到达json中的php数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7060957/