0

I have a php array like this one :

Array
(
    [0] => banana, peach, cherry
    [1] => strawberry, apple, lime
)

I pass it to Jquery using json_encode($myArray)

In Jquery I receive my array like this : ["banana, peach, cherry","strawberry, apple, lime"]

Now I wanna extract each value : "banana, peach, cherry" & "strawberry, apple, lime"

When I try to use this :

$.each(data, function(key, value){
    alert(value);
});

it alerts me each chars : [ " b a n a n................ etc instead each value.

Do you know why ?

EDIT :

This is how I receive my data from php :

$.post('ajax/fruits.php', function(data) {
    var obj = $.parseJSON(data);
    var chunks = obj['chunks'] // gives me : ["banana, peach, cherry","strawberry, apple, lime"]
    if (obj['error']==0) {
        mix_fruits(chunks); // a function that should extract each value
    }
});
Xavier C.
  • 1,809
  • 4
  • 24
  • 40
  • 3
    Try parsing the json with the jQuery-function $.parseJSON(). var parsedData = $.parseJSON(data); $.each(parsedData, function(key, value) { ..... – ninja Sep 04 '13 at 09:09
  • Your Ajax call should be treating the received data as JSON and converting it to an JS object automatically, but it looks like it is treating it as a plain string and not converting it. Please add to the question your code for your actual Ajax call; this will help us debug the problem for you. – Spudley Sep 04 '13 at 09:18
  • I have updated my code, in case. – Xavier C. Sep 04 '13 at 09:44
  • @SergentPepper: Well, what does `mix_fruits` do? – Jon Sep 04 '13 at 10:15
  • @Jon : In fact, data returns 2 objects -> error and chunks. mix_fruits should decompose chunks object in order to get the 2 values separatly : "banana, peach, cherry" & "strawberry, apple, lime". – Xavier C. Sep 04 '13 at 10:26
  • @SergentPepper: Either `chunks` is OK (by your own description) and whatever the problem is, it is inside `mix_fruits` **OR** `chunks` is in fact not OK because you are passing it wrong from PHP (and the description is misleading). In either case, there is not enough information in the question to determine what's wrong. And we really should not have to beg you for it. – Jon Sep 04 '13 at 10:54

2 Answers2

3

You don't show how you are passing and receiving the data, but somewhere in the process you are making a mistake.

It is evident from the description that data is a string, not an array as it should have been based on the PHP variable. And since the string begins with the characters that make up the JSON representation of your data, this means the JSON is being wrapped into a string instead of parsed as a JavaScript literal.

Assuming that passing/receiving is not done through an AJAX request (in which case jQuery would almost certainly parse the data automatically) I 'm guessing that you are doing this:

var data = '<?php echo json_encode($data); ?>';

while you should instead be doing this:

var data = <?php echo json_encode($data); ?>; // no quotes!
Jon
  • 428,835
  • 81
  • 738
  • 806
2

You need to parse the JSON object before you can use it,

var jsonObj = jQuery.parseJSON(data);

then to loop through each item use this,

for(var key in jsonObj)
{
    curr = jsonObj[key];
}

And if you want to use non-jquery one,

Parse JSON in JavaScript?

Community
  • 1
  • 1
Hendyanto
  • 335
  • 1
  • 8