.length计算字符数,而不是数组长度

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.

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.