0

I am trying to pass an array with json in php:

$array = array ($a, $b, $c);
echo json_encode($array);

and to use it in jquery like this:

$(function () {
    $.ajax({
        url: 'api.php',
        data: "",
        dataType: 'json',
        success: function (data) {
            var id = data[3];
            var vname = data[1];
            $('#description').html("<b>id: </b>" + id + "<b> name: </b>" + vname);
        }
    });
});

But this prints as "id: [object Object] name: [object Object]" instead of the values I want, I get that..

What am I doing wrong? Thank you for any help..

palaѕн
  • 72,112
  • 17
  • 116
  • 136
Adrian M.
  • 7,183
  • 15
  • 46
  • 54
  • It seems like `$a`, `$b` and `$c` are objects (or associative arrays). `[object Object]` is **not** an error, it's the default string representation of an object. But, `id` should be `undefined`, since your array has only three elements. Anyways, we cannot really help you without knowing the data. – Felix Kling Jun 13 '13 at 14:08
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Jun 13 '13 at 14:11

1 Answers1

1

[object Object] means that $a, $b and $c are objects. To print out an object in javascript you should use data[index].variable where variable is the property of the object. Try to console.log(data) to see what you get: you should see 3 objects. Expand them, and read the variables inside of them.

Also, since you're array is made of 3 elements, data[3] should not exists. You should post more PHP code.

Saturnix
  • 10,130
  • 17
  • 64
  • 120