I return an array of array from php to json
here's php array
$cities = array();
while($row = mysql_fetch_array($result)){
$cityRow= array('cityNo'=>$row['city_no'], 'cityName'=>$row['city_name']);
$cities[]=$cityRow;
}
echo json_encode($cities);
Here's the json
$.getJSON("controllers/Customer.controller.php",param,function(result){
// what should I write here to reach the array elements??
});
You can iterate over the object using .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
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With