Simpanoz
Simpanoz

Reputation: 2859

jQuery.each how to get values from PHP array

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.

Iam getting following json response:

    {"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'

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

Thanks in advance

Upvotes: 0

Views: 679

Answers (2)

Teneff
Teneff

Reputation: 32158

Example how to create unordered list with sEcho

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

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

Upvotes: 1

ArtoAle
ArtoAle

Reputation: 2977

you can do:

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

Assuming that $json_array helds your json string :D

enjoy!

Upvotes: 0

Related Questions