2 Replies - 586 Views - Last Post: 21 July 2013 - 12:36 AM

#1 Planet Telex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-November 10

.length does not work with array returned from ajax call

Posted 20 July 2013 - 06:02 PM

Hello all,

I'm returning a two-dimensional array using json_encode. I'm able to output individual elements of the array, but when I try to use array1.length, it returns undefined. I'm assuming it is because it is an array of objects? Any help would be greatly appreciated. Here is the code...

$.ajax({
			   type: "POST",
       		   url: "InputTestimonial.php",
        	   data: postData,
        	   dataType : "json",               
        	   success: function(json){
        	   var array1 = json;
        	   alert(array1.length); // doesn't work
               console.log(array1[0]['firstName']);
               console.log(array1[0]['lastName']);
               console.log(array1[0]['text']);
               console.log(array1[0]['date']);
               
               },


 
$myJsons = array();
 
  while($row = mysql_fetch_assoc($result))
  {
  	$myJsons[$i] = array(
  		
  			  firstName => $row['firstname'],
			  lastName => $row['lastname'],
			  text => $row['text'],
			  date => $row['date']
			
  	);
echo json_encode($myJsons, JSON_FORCE_OBJECT);


This post has been edited by Planet Telex: 20 July 2013 - 06:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: .length does not work with array returned from ajax call

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2884
  • View blog
  • Posts: 9,569
  • Joined: 12-December 12

Re: .length does not work with array returned from ajax call

Posted 20 July 2013 - 07:21 PM

Yes, an object doesn't have a length. If you don't need to support IE8 or below you could use:

var count = Object.keys(array1).length;

Or you can iterate the object:

var key, count = 0;
for(key in array1) {
  if(array1.hasOwnProperty(key)) {
    count++;
  }
}

I'm not sure if this is exactly right, I haven't tested.

Or make $myJsons a (single) object that contains an array, then .length would work.

Sorry, it's late, and my answer needs checking.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3393
  • View blog
  • Posts: 9,590
  • Joined: 08-June 10

Re: .length does not work with array returned from ajax call

Posted 21 July 2013 - 12:36 AM

View PostPlanet Telex, on 21 July 2013 - 03:02 AM, said:

 
$myJsons = array();
 
  while($row = mysql_fetch_assoc($result))
  {
  	$myJsons[$i] = array(
  		
  			  firstName => $row['firstname'],
			  lastName => $row['lastname'],
			  text => $row['text'],
			  date => $row['date']
			
  	);
echo json_encode($myJsons, JSON_FORCE_OBJECT);


I wonder if AJAX got a decent response for that at all.
- $i is neither defined, nor increased
- while() loop is not closed
- array keys are missing quotation marks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1