0

When I pass an php array to jquery

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

Jquery script

$.post(
    "http://localhost/xiuno",
    {useranswers:arr,shijuanid:shijuanid},
    function(data){
        alert(data[0]);
    }

It shows nothing,so,how to access data by using index?

Also there's one thing confused me, I changed alert(data[0]); to alert(data); it shows ["a","b","c"], it is neither a JSON type like ["0":"a","1":"b","2":"c"] or an array, cause it can not be accessed by index,so, what exactly is the data retrieved from PHP script? It was supposed to be JSON, but not.

Also,I'm wondering is there a way to pass array directly from PHP, the following code does not work

$testArr = array('a','b','c');
echo $testArr;

Because an array can not be echoed, so how to do it?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user7031
  • 435
  • 4
  • 16

3 Answers3

1

The output ["a","b","c"] is correct. That's how arrays are represented in JSON (["0":"a","1":"b","2":"c"] is completely invalid).

That also means that data is a string and you have to parse the JSON into a JavaScript array first:

data = $.parseJSON(data);
alert(data[0]);

If you don't do this, data[0] will access the first character of the string, which is [.

what exactly is the data retrieved from PHP script?

JSON, which is a textual data-exchange format, just like XML, CSV or YAML. It is a way to encode data. In order to access the data, it has to be converted to native data types first. That's what you do with $.parseJSON. It takes a string containing JSON and returns an object or array.

I'm wondering is there a way to pass array directly from PHP

No there is not. A PHP array is a data structure that can only be understood by PHP. In order to pass the data to another system/program/etc, you first have to encode the data in a way that is understood by the other side. JSON is a good choice for that.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thanks,so it means the data in the callback function is actually the raw JSON type,am I right?But when I send javascript array to PHP:{useranswers:arr,shijuanid:shijuanid} , there was no need to parse it in the php script using json_decode(),but it looks like it was JSON type? – user7031 Jun 29 '13 at 15:42
  • Kind of, but I would not express it like that. JSON is *text*. You can have text in form of *strings* in JavaScript (and all the other languages). Hence `data` is a string containing JSON. – Felix Kling Jun 29 '13 at 15:43
  • But when I send javascript array to PHP:{useranswers:arr,shijuanid:shijuanid} , there was no need to parse it in the php script using json_decode(),but it seems like it was JSON type,which is not,any idea? – user7031 Jun 29 '13 at 15:47
  • That is not JSON, it's an *object literal*. They look similar but they are not the same. jQuery does the encoding for you so you don't have to do it. Lets assume you pass the object `{foo: 'bar', baz: 42}`, then jQuery will create the query string `foo=bar&baz=42` from it. From the [`$.ajax` documentation](http://api.jquery.com/jQuery.ajax/) about the data option: *"Data to be sent to the server. It is converted to a query string, if not already a string."* – Felix Kling Jun 29 '13 at 15:49
0

The PHP function json_encode() returns a string of JSON formatted text. When you receive this string in your JavaScript code, you should first parse it back to an object.

Parsing JSON to JS code goes like this

//The JS way:    
data = JSON.parse(json);

//The jQuery way:
data = $.parseJSON(data);

From: Parse JSON in JavaScript?

I hope this works for you.

Community
  • 1
  • 1
Rick Slinkman
  • 643
  • 10
  • 23
0

you can use JSON.parse

var jsonData = JSON.parse(data)

AnonGeek
  • 7,408
  • 11
  • 39
  • 55