Smudger
Smudger

Reputation: 10781

loop through jquery array using the index to return the value

I have the below jquery array being returned by a PHP server. if I alert the array with alert(data) it outputs:

Array
(
    [0] => Array
        (
            [firstname] => john
            [lastname] => paul
            [id] => 123     
        )
    [1] => Array
        (
            [firstname] => adam
            [lastname] => james
            [id] => 343     
        )
)

I have tried using: var i; for (i = 0; i < result.length; ++i) { alert(result[i]); }

This returns a single character. I need the entire value to be alerted.

for example: John then paul then 123 then adam... etc etc

Thanks as always,

Upvotes: 0

Views: 49

Answers (1)

Anto S
Anto S

Reputation: 2449

Please try this;

assume result will be the variable which holding all values

<script>
    data = JSON.parse(result);
    $.each(data, function(key, value){
         alert(value.firstname);
         alert(value.id);
    });
<script>

if you are getting this result from ajax make below adjustment.

<script>
$.ajax({
     .
     .
     dataType: 'JSON',
     .
     .
});
</script>

Upvotes: 1

Related Questions