2

I'm trying to understand how my PHP script can pass an array to my JavaScript code. Using the following PHP, I pass an array:

$c = array(3,2,7);

echo json_encode($c);

My JavaScript is as follows:

    $.post("getLatLong.php", { latitude: 500000},
        function(data) {
            arrayData = data
            document.write(arrayData)
            document.write(arrayData[0]);
            document.write(arrayData[0]);
            document.write(arrayData[0]);
        });
</script>

What is printed out on the screen is

[3,2,7][3,

I'm trying to understand how json_encode works - I thought I would be able to pass the array to a variable, and then access it like a normal JavaScript array, but it views my array as one large text string. How do ensure that it reads it like an array?

5
  • Your example is a 2-dimensional array. Try using data[0][0], [0][1],[0][2], etc. to access 3,2,7 respectively. Commented Jan 15, 2011 at 20:27
  • hmm, I understand now that it is a 2D array. but even if I change it, it still reads it as a string. Commented Jan 15, 2011 at 20:29
  • @Brad Christie: It is not at all a 2-dimensional array. What makes you think that? Commented Jan 15, 2011 at 20:41
  • @Felix: Look a their revision history. Their first code implemented (what's being referred to as $c) as array(array(3,2,7)). But, I take full accountability for missing that they didn't specify they wanted JSON parsed (though I can't confirm if they included a header() in the AJAX page specifying JSON that .post wouldn't take care of it automatically) Commented Jan 15, 2011 at 20:44
  • @Brad Christie: Oh I see. Nevermind then :) Commented Jan 15, 2011 at 20:45

1 Answer 1

4

Pass the dataType argument to $.post:

$.post("getLatLong.php", { latitude: 500000},
   function(data){         
   // ...
}, 'json');

Then data will be properly decoded.

Alternatively you can also use $.parseJSON in the callback.

Explanation of JSON:

JSON is a data exchange format. It specifies the structure of the data string that is transmitted. json_encode takes a PHP data structure like an array or an object and transformes it to JSON, i.e. a string with a certain format.

json_encode($c) will give you the string "[3,2,7]".

On the client site, you receive that string, but you have to decode it in to proper JavaScript data structures. So $.parseJSON(data) will give you a JavaScript array with 3 elements (or if you pass 'json' as third parameter to .post(), jQuery is doing the decoding automatically for you).

Sign up to request clarification or add additional context in comments.

4 Comments

As it turns out, if they placed header('Content-Type: application/json') it appears .post would have worked correctly (or at least not needed to be explicitly noted it's a JSON reply [which makes sense])
@Brad where would I place the header? Just trying to understand this overall process... as the code is now working.
place the header in getLatLong.php
@celenius: ... before any output.

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.