using ajax with php, getting the LENGTH of the array

using ajax with php, getting the LENGTH of the array

I am using jquery (.post) to send user input to a php script which returns an array. If I return the array without defining the keys (see the bottom line of the PHP below) the returnArray.length correctly gives the length of 3. However, if I try to define custom keys (the first line of PHP) returnArray.length has a value of "undefined".

Is there some special way to reference the length property when the keys are customized? I know I can just define two arrays in PHP, but one array is neater, and I want to know if I am messing something up.

I am using the following js:

  1. $(document).ready(function(){
  2.             $('#txtValue').keyup(function(){
  3.                 sendValue($(this).val());     
  4.             }); 
  5.             
  6.         });
  7.         function sendValue(str){
  8.             $.post("ajaxtest.php",{ sendValue: str },
  9.             function(data){
  10.                 returnArray = data.returnValue;
  11.                 $('#display').html("Length: " + returnArray.length + " " + returnArray[0]);
  12.             }, "json");
  13.             
  14.         }

Along with the following php

  1. //THIS DOES NOT ALLOW THE USE OF "LENGTH"
  2. //echo json_encode(array("returnValue"=>array("001"=>"This is 001 - $value", "003"=>"This is 002 - $value", "005"=>"This is 003 - $value")));
  3. //THIS WORKS
  4. echo json_encode(array("returnValue"=>array("This is 001 - $value", "This is 002 - $value", "This is 003 - $value")));