php - 到达json中的php数组元素

标签 php jquery ajax json

我从 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/

相关文章:

php - 如何显示 cron 作业的计时器

php - 使用 jquery 调用 PHP 函数 $.ajax 返回 json

javascript - 将数据从 jquery ajax 添加到 JSON 对象

具有不同输入 ID 的 jQuery 多重自动完成

ruby-on-rails - 使用 remote=true 执行 ajax 时,Rails 不保留 session 数据

php - 选择框,在 WordPress 中使用自定义字段和自定义帖子类型对帖子进行排序?

php - 从mysql JSON获取数据到Listview从模型获取ID

php - DOM 中的类未更新,处理程序无法正常工作

javascript - 选择文档中除 div 元素之外的所有元素并对其应用 css

javascript - AJAX 返回错误且响应文本为空