"fiv...">

$ .ajax get object after json_encode ($ arr) from php, but how to get key and value in jQuery?

1

In PHP:

$arr = array( 10=>"ten", 5=>"five", 2=>"two"); return json_encode($arr);

In JS - $ .ajax ():

success: function(data){ console.log(data);}

2

What I see in the console:

Object {2: "two", 5: "five", 10: "ten"},

I want to use for(var i=0; i< data.length,i++), but failed.

Finally, it works as follows: for(var i in data)

3

My question is: why is the array sorted? I want the array to be kept unsorted.

Does anyone help me?

+4
source share
1 answer

JSON , , .
, , , , - .
2

$arr = array( 'indecies'=>array(10,5,2), 'values'=>array("ten","five","two") ); 
return json_encode($arr);
for(var i=0; i< data.indecies.length,i++){
    // do something with
    //data.indecies[i]
    //data.values[i]
}
+1

All Articles