0

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! :)

meagler
  • 287
  • 1
  • 2
  • 11
  • you have a single array and each array element is a javascript object with properties `name` and `lev`. Use object notation to access them ...while iterating array and don't use `Object.keys()` on an array – charlietfl Jan 29 '17 at 18:23
  • make sure to set `dataType:'json'` on ajax request also – charlietfl Jan 29 '17 at 18:26
  • Also be sure you are using `json_encode()` on the php array...javascript can't read var dump of php array – charlietfl Jan 29 '17 at 18:29

1 Answers1

0

Use JSON.parse to convert the string to JSON Object.

    response = JSON.parse(response)
    alert("response="+response);
    var array_length = Object.keys(response).length;                
    alert("array length="+array_length);

    alert("response[0][0]="+response[0][0]);
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
  • no need to parse yourself when you set proper dataType and array element objects have no property `[0]` – charlietfl Jan 29 '17 at 18:25
  • Hi @Vladu Ionut thank you very much for the reply...I tried what you suggested and the output I got for the array length is indeed 7 but for response[0][0], I got the output as undefined..any ideas why? – meagler Jan 29 '17 at 18:26
  • @KlaraNewbie for exact same reason I mentioned above ... the array elements are objects not arrays in javascript – charlietfl Jan 29 '17 at 18:26
  • @charlietfl Oh okay, so how should I work around this? – meagler Jan 29 '17 at 18:29
  • Read the linked answer/tutorial...you need to iterate the main array and access each **object** within that loop – charlietfl Jan 29 '17 at 18:30
  • So i tried it and I get this error : `SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data` This error is on the line `response = JSON.parse(response);` – meagler Jan 29 '17 at 18:34
  • Okay I found it...`dataType:'json'` is causing the error – meagler Jan 29 '17 at 18:36