Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4608746
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:53:20+00:00 2026-05-22T00:53:20+00:00

I have a problem with getting the length of an array, when it is

  • 0

I have a problem with getting the length of an array, when it is not initialized in the format: var variable = new Array();

Here is my code:

var inx;
var qns;
var qis;
var ncs;
var nzs;
var tps;

function display_question()
{

   $( "#question_no" ).text( qns[ inx ] );
   $( "#question_nc" ).text( ncs[ inx ] );
   $( "#question_nz" ).text( nzs[ inx ] );
   $( "#the_question" ).hide();
   $( "#the_question" ).text( tps[ inx ] );
   $( "#the_question" ).fadeIn( 500 );

}

function next_question()
{

   var arr_len = qns.length;

   if( inx < arr_len )
   {

      inx++;
      display_question();

   }

}

function prev_question()
{

   if( inx > 0 )
   {

      inx--;
      display_question();

   }

}

function get_questions()
{

   var url = "php/pytania.php"; 

   $.ajax(
   {

      cache: false,
      async: false,
      type: "GET",
      dataType: "text",
      url: url,
      success: function( response ) 
      { 

         data = jQuery.parseJSON( response );

         inx = 0;
         qns = data.qns;
         qis = data.qis;
         ncs = data.ncs;
         nzs = data.nzs;
         tps = data.tps;
         display_question();

      }

   } );

}

The problem is that when I try to return the length of qns like so: qns.length, it doesn’t return anything. I suspect that this may be because when i read in the array from the JSON response, it is not as an array object. Is there any way to fix this? I’d appreciate any help! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:53:21+00:00Added an answer on May 22, 2026 at 12:53 am

    There is no difference between arrays/objects you retrieve via JSON and those created in JavaScript. The JSON objects and arrays are parsed into JavaScript ones.
    And there is actully no need to call new Array() ever.

    It seems data.qnr is an object not an array. You would have to loop over the objects and count the elements yourself.

    However as it seems that the object is continuous numerical keys, it would be better to create the correct output (array) from the beginning.

    In your comments you wrote you use json_encode( $json_array, JSON_FORCE_OBJECT ) to create the JSON output. This will convert every array, even numerical ones, into objects.

    Instead, just use json_encode($json_array). By default PHP will convert associative arrays to objects in JSON and numerical arrays to … well, arrays 😉

    If you do this, data.qnr will work as expected in JavaScript.


    Also note, instead of dataType: 'text', you can set the it to json and jQuery will parse the response automatically for you (so no need to call jQuery.parseJSON).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with this question from my professor. Here is the question:
Hi the problem goes like this: I have in CoreData entities that have a
first post here so sorry for the length of it. I've been lurking and
I'm writing an API for a website which doesn't have it. Basically, my PHP
I have a simple form (textbox, submit button) which is wrapped in an update
I am working with a countdown timer and have it set so that it
I am using Sencha Touch to develop a mobile version of a Bus Tracker
Ok, first of all, I originally programmed this csv parser in PHP. It is
How do I create a RichTextEdit using RIM 4.5 API that contains text with
I m using network service so when i click on button in my first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.