admin管理员组

文章数量:1410682

I want to count the items of an array returned by json. When I use response.length, it counts all the characters in the array(I guess so) while what I want it to do is to count how many items are there in the array. Same method works in another pages, just not with this one.

This is the php code:

            ...$response[] = array("id" => $row['appid'],
                            "hour" => $row['hour'],
                            "tname" => $tname,
                            "tsurname" => $tsurname,
                            "cname" => $cname,
                            "csurname" => $csurname,
                            "cgsm" => $cgsm,
                            "cemail" => $cemail,
                            "cdept" => $cdept,
                            "cdeg" => $cdeg,
                            "purpose" => $purpose,
                            "clientnotshown" => $row['clientnotshown']);


    };

    if(isset($response)){

    echo json_encode($response);

    } else {

    $response = array("val" => 0);
    echo json_encode($response);

    };

Javascript code:

function updateTable() {

var getData = {
                      date: $("#displaydate").val(),
                      operation:"getData"
                  }

                  $.post( "printoperations.php", getData).done(function( response ) {
                      if (response.val != 0){

                          alert("so far so good")
                          var arrayLength = response.length
                          alert(response)
                          alert(arrayLength)}
};

Here is a picture of what I get. I want to get the count of items, which in this case is 2.

I want to count the items of an array returned by json. When I use response.length, it counts all the characters in the array(I guess so) while what I want it to do is to count how many items are there in the array. Same method works in another pages, just not with this one.

This is the php code:

            ...$response[] = array("id" => $row['appid'],
                            "hour" => $row['hour'],
                            "tname" => $tname,
                            "tsurname" => $tsurname,
                            "cname" => $cname,
                            "csurname" => $csurname,
                            "cgsm" => $cgsm,
                            "cemail" => $cemail,
                            "cdept" => $cdept,
                            "cdeg" => $cdeg,
                            "purpose" => $purpose,
                            "clientnotshown" => $row['clientnotshown']);


    };

    if(isset($response)){

    echo json_encode($response);

    } else {

    $response = array("val" => 0);
    echo json_encode($response);

    };

Javascript code:

function updateTable() {

var getData = {
                      date: $("#displaydate").val(),
                      operation:"getData"
                  }

                  $.post( "printoperations.php", getData).done(function( response ) {
                      if (response.val != 0){

                          alert("so far so good")
                          var arrayLength = response.length
                          alert(response)
                          alert(arrayLength)}
};

Here is a picture of what I get. I want to get the count of items, which in this case is 2.

Share Improve this question edited Feb 23, 2017 at 6:32 Ahmet asked Feb 23, 2017 at 6:23 AhmetAhmet 11511 bronze badges 4
  • 1 parse that response to object and then get count of it. – Brave Soul Commented Feb 23, 2017 at 6:25
  • 1 Have you tried parsing the JSON to get an actual array? Or specify 'json' as the data type when you call $.post() and jQuery will parse it for you. – nnnnnn Commented Feb 23, 2017 at 6:25
  • 1 JSON.parse(response), or tell jQuery that you expect a JSON response. – Sven Commented Feb 23, 2017 at 6:27
  • sorry for being so ignorant, but I don't know how to do that. – Ahmet Commented Feb 23, 2017 at 6:28
Add a ment  | 

6 Answers 6

Reset to default 3

Whatever you send back from PHP is always a string.
jQuery will however parse that string for you automagically, if you either send the correct headers, or tell it that it's JSON in the settings

$.post("printoperations.php", getData, function(response) {
    if (response.val != 0) {
        console.log("so far so good")
        var arrayLength = response.length
        console.log(response)
        console.log(arrayLength)
    }
}, 'json'); // <- here

And use the console for debugging

You need to parse the response var response = JSON.parse(response) and then response.length will return the desired result.

var getData = {
                  date: $("#displaydate").val(),
                  operation:"getData"
              }

              $.post( "printoperations.php", getData).done(function( response ) {
                  if (response.val != 0){

                      alert("so far so good")
                      var responseArray = JSON.parse(response)
                      alert(responseArray)
                      alert(responseArray.length)}
};

Seems you are trying to find length of string. Instead, try to alert(typeof response), If it gives string, then before doing anything parse it to JSON with JSON.parse(response) and then try.

JSON.parse(response)That did the trick. Thanks for all answers!

You should put this

header('Content-Type: application/json');

in your server side php query.

本文标签: javascriptlength counts charactersnot the array lengthStack Overflow