jQuery.each如何从PHP数组中获取值

时间:2021-11-13 01:02:25

I have following array return from server side:

我从服务器端返回以下数组:

[json] => Array
        (
            [0] => {"sEcho":1,"iTotalRecords":"4","iTotalDisplayRecords":"4","bid":"22","aaData":[[1,"Monetary Bid name","12","4.00","6.00","-",".25\/50","-"],[2,"Monetary Bid name","14","5.00","8.00","-","1\/2","-"],[3,"Monetary Bid name","12","87.00","52.56","-","-","-"],[4,"Monetary Bid name","42","22.00","33.00","LH","3\/6","1"]]}
            [1] => {"sEcho":1,"iTotalRecords":"2","iTotalDisplayRecords":"2","bid":"23","aaData":[[1,"Second Challenge Bid","12","5.00","4.00","NLH",".10\/.20","1"],[2,"Second Challenge Bid","15","7.00","4.00","PLO",".25\/50","1"]]}
            [2] => {"sEcho":1,"iTotalRecords":"0","iTotalDisplayRecords":"0","bid":"24","aaData":[]}
        )

How can I get/iterate values of this array through jQuery each function.

如何通过jQuery每个函数获取/迭代此数组的值。

Iam getting following json response:

我得到了json的回应:

    {"sEcho":1,"iTotalRecords":"4","iTotalDisplayRecords":"4","bid":"22","aaData":[[1,"Monetary Bid name","12","4.00","6.00","-",".25\/50","-"],[2,"Monetary Bid name","14","5.00","8.00","-","1\/2","-"],[3,"Monetary Bid name","12","87.00","52.56","-","-","-"],[4,"Monetary Bid name","42","22.00","33.00","LH","3\/6","1"]]},

{"sEcho":1,"iTotalRecords":"2","iTotalDisplayRecords":"2","bid":"23","aaData":[[1,"Second Challenge Bid","12","5.00","4.00","NLH",".10\/.20","1"],[2,"Second Challenge Bid","15","7.00","4.00","PLO",".25\/50","1"]]},

{"sEcho":1,"iTotalRecords":"0","iTotalDisplayRecords":"0","bid":"24","aaData":[]}

How can I get values of 'iTotalRecords', 'aaData' etc through each loop. Currently Iam using following loop, but it is returning 'undefined'

如何通过每个循环获取'iTotalRecords','aaData'等值。目前Iam使用以下循环,但它返回'undefined'

jQuery(json).each(function(k, v){
    alert(json.aaData[k][2]);
});

Thanks in advance

提前致谢

2 个解决方案

#1


1  

Example how to create unordered list with sEcho

示例如何使用sEcho创建无序列表

$list = $(body).append('<ul></ul>');

$.each(data['json'], function(k, v){
    $list.append($('<li></li>').html(v.sEcho));
});

#2


0  

you can do:

你可以做:

<script>
    var = <?php echo $json_array?>
    $(var).each(function(){...}); 
</script>

Assuming that $json_array helds your json string :D

假设$ json_array持有你的json字符串:D

enjoy!

#1


1  

Example how to create unordered list with sEcho

示例如何使用sEcho创建无序列表

$list = $(body).append('<ul></ul>');

$.each(data['json'], function(k, v){
    $list.append($('<li></li>').html(v.sEcho));
});

#2


0  

you can do:

你可以做:

<script>
    var = <?php echo $json_array?>
    $(var).each(function(){...}); 
</script>

Assuming that $json_array helds your json string :D

假设$ json_array持有你的json字符串:D

enjoy!