0

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,

1
  • 1
    Try to convert returned result like result = JSON.parse(result) Commented Apr 14, 2015 at 10:36

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks,but I don't want to call the column / field name directly. rather than alert(value.firstname) I would like to use alert(value[1]) or the like.
Then you can simply add JSON.parse(result); on your existing system just before the for loop and check a @Satpal suggested
I tried that method also, but JSON will be the reliable method for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.