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:
- $(document).ready(function(){
- $('#txtValue').keyup(function(){
- sendValue($(this).val());
- });
-
- });
- function sendValue(str){
- $.post("ajaxtest.php",{ sendValue: str },
- function(data){
- returnArray = data.returnValue;
- $('#display').html("Length: " + returnArray.length + " " + returnArray[0]);
- }, "json");
-
- }
Along with the following php
- //THIS DOES NOT ALLOW THE USE OF "LENGTH"
- //echo json_encode(array("returnValue"=>array("001"=>"This is 001 - $value", "003"=>"This is 002 - $value", "005"=>"This is 003 - $value")));
- //THIS WORKS
- echo json_encode(array("returnValue"=>array("This is 001 - $value", "This is 002 - $value", "This is 003 - $value")));