1.11M Members

PHP associative array to js array

 
0
 

Hi there,
I m using jquery ajax function to get the data from server and i m getting the data in associative array on success, but now i want to convert that associative array in javascript, how i can convert PHP associative array in javascript array.

Waiting for your quick reply
thanks

the associative array that i get on success is

Array(    
[0] => Array        (            [q_id] => 83            [t_id] => 4            [question] => jjjjs.jfdaskldjf            [option1] => jjjjasdfasdf            [option2] => jjj            [option3] => lll            [option4] => lll            [answer] => lll            [marks] => 22        )   

[1] => Array        (            [q_id] => 84            [t_id] => 4            [question] => This is testing            [option1] => 2            [option2] => 7            [option3] => 8            [option4] => 9            [answer] => 2            [marks] => 2        )    

[2] => Array        (            [q_id] => 85            [t_id] => 4            [question] => hello this is another test            [option1] => a            [option2] => b            [option3] => c            [option4] => d            [answer] => a            [marks] => 2        )    

[3] => Array        (            [q_id] => 86            [t_id] => 4            [question] => another test            [option1] => 8            [option2] => 9            [option3] => 0            [option4] => 1            [answer] => 1            [marks] => 2        )    [4] => Array        (            [q_id] => 87            [t_id] => 4            [question] => last question             [option1] => z            [option2] => x            [option3] => c            [option4] => v            [answer] => c            [marks] => 2        ))1
 
0
 

I also try the json_encode and the data that i m getting on success is

{"o":[{"q_id":"83","t_id":"4","question":"jjjjs.jfdaskldjf","option1":"jjjjasdfasdf","option2":"jjj","option3":"lll","option4":"lll","answer":"lll","marks":"22"},{"q_id":"84","t_id":"4","question":"This is testing","option1":"2","option2":"7","option3":"8","option4":"9","answer":"2","marks":"2"},{"q_id":"85","t_id":"4","question":"hello this is another test","option1":"a","option2":"b","option3":"c","option4":"d","answer":"a","marks":"2"},{"q_id":"86","t_id":"4","question":"another test","option1":"8","option2":"9","option3":"0","option4":"1","answer":"1","marks":"2"},{"q_id":"87","t_id":"4","question":"last question ","option1":"z","option2":"x","option3":"c","option4":"v","answer":"c","marks":"2"}]}

Now how i can get the values against q_id 83

 
0
 

Most likely:

data['o'][0]
//or
data[0]
 
0
 

I also try this but error is Uncaught TypeError: Cannot read property '0' of undefined

now what should i do

 
0
 

Show your AJAX code.

 
1
 

problem solved i only add dataType: 'json' and now its working fine

Thanks

Question Answered as of 1 Year Ago by pritaeas
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: