Click here to Skip to main content
15,741,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys i would like to ask how do you count the size of an array? i have an array that gets its value from the json_encode value passed by my php file... here is the format of my php return json data

$idArr= array();
while($row= mysql_fetch_array($outputArrayResult)){			
  $idArr["" . $row['id']] = false;

echo json_encode(array("total_pages" => $num_pages,"ids" => $idArr));

which returns in this format
/* firebug output */


now in my javascript i handle the ajax data returned using this code

/* ajax request success handler */
success: function(data){	
  var obj = jQuery.parseJSON(data);
  var ids= obj.ids;
  alert("this is " + ids.length + " -- " + ids["2"]);

as you can see im retrieving the data using jQuery.parseJSON(data) and pass it to my variable ids. now when i try to alert the value it gives me this output

/* alert message */
this is undefined -- false

so it means it successfully retrieved the value of ids["2"] but i cant get the length of the array to output properly... how do you do this?

Just to clarify the correct answer by Tanveer A:

You need to use the property length; please see:[^].

This will only work for "traditional" arrays indexed by integers.

For a general case of an object, one should count its "own" members:

count = 0;
for(index in x)
    if (x.hasOwnProperty(index))

All JavaScript objects (not counting the primitive objects like numeric of course) are associative arrays. Please see the section "object based" here:[^].

See also:[^].

Share this answer
The problem is that ids is not an array. Javascript does not have associative arrays, just anonymous objects.

So you'll have to count the members if you really need to know how many there are:
var x, count = 0;
for(x in ids)
Share this answer
Sergey Alexandrovich Kryukov 22-Feb-12 14:44pm    
In fact, all JavaScript objects are associative arrays.
Please see the section "object based" here:

See also:

So, I can only vote a big 1. Please double check before confusing people with something which is not true.
Graham Breach 22-Feb-12 16:55pm    
OK, I stand corrected on the objects not being associative arrays part.

But they still don't support .length - and I did check using the console in Chrome and FireFox:

> var x = [1,2,3]; x.length;
> var x = {a:1,b:2,c:3}; x.length;
Sergey Alexandrovich Kryukov 23-Feb-12 1:15am    
Oh, you are right!

count = 0;
for(index in x)
if (x.hasOwnProperty(index))

will return the length (number of members, actually). This is still an associative array, indexed by 'a', 'b', 'c'.
Sergey Alexandrovich Kryukov 23-Feb-12 1:16am    
Therefore, I up-voted the answer. Sorry that I missed the point if first place.
Madzmar25 23-Feb-12 0:43am    
Yes that was my last resort.... i cant figure out how to get the length so i had to iterate every element in my array just to get the size... thanks again for the answer
to get your array length arrayname.length
Share this answer
Sergey Alexandrovich Kryukov 22-Feb-12 14:50pm    
Of course! What else should it be. As you were the only one who answered without telling a lie (which is amazing, considering such a simple problem :-), I voted 5.
I added some more information in my answer, credited your answer.
Did you try Googling "Javascript array length"? I got this as the very first result:[^]
Share this answer
Madzmar25 22-Feb-12 0:16am    
yes i just did that... as you can see on my first post
alert("this is " + ids.length + " -- " + ids["2"]); where ids = array
krumia 22-Feb-12 0:18am    
Oops so sorry. Can't figure out how I missed that :D
krumia 22-Feb-12 0:33am    
See if this is a similar kind of problem:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900