So what I'm doing is that I'm echoing a multidimensional array from a php file called by an ajax function.
This is what the array in the php file looks like:
Array
(
[0] => Array
(
[username] => klara
[lev] => 0
)
[1] => Array
(
[username] => mira
[lev] => 0
)
[2] => Array
(
[username] => coursera
[lev] => 0
)
[3] => Array
(
[username] => chang
[lev] => 2
)
[4] => Array
(
[username] => jack
[lev] => 3
)
[5] => Array
(
[username] => elon
[lev] => 4
)
[6] => Array
(
[username] => musk
[lev] => 7
)
)
My Ajax success function looks as follows:
success: function (response)
{
alert("response="+response);
var array_length = Object.keys(response).length;
alert("array length="+array_length);
alert("response[0][0]="+response[0][0]);
}
So the output is as follows:
response=[{"username":"klara","lev":0},{"username":"mira","lev":0},{"username":"coursera","lev":0},{"username":"chang","lev":2},{"username":"jack","lev":3},{"username":"elon","lev":4},{"username":"musk","lev":7}]
array length=204
response[0][0]=[
So what is happening is that "array length" is getting the length of the response instead of the number of rows and if I put a for loop and try to extract the elements then it extracts every single character in the response.
So how should I get the number of rows in the response and how should I get the values of username. That is, I wish to get the number of rows as 7 and extract the names "klara","mira",etc. How should I do it? I'm really new to this so any help would be appreciated. Thanks a lot!
Thanks a lot! :)