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

  • SEARCH
  • Home
  • 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 7513787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:17:37+00:00 2026-05-30T00:17:37+00:00

I have a multidimensional array like this Array ( [1] => Array ( [product_id]

  • 0

I have a multidimensional array like this

Array
(
    [1] => Array
        (
            [product_id] => 1
            [product_model] => HFJ5G1.5
            [product_type] => plat
            [product_return] => graviteits

        )

    [2] => Array
        (
            [product_id] => 2
            [product_model] => HHJ5S2.5
            [product_type] => holle plunjer
            [product_return] => veer       

        )
    );  //Only 2 are shown here i have around 110 values

And i encoded this to json by

json_encode($array);

The resulted jsonString is something like this

{"1":{"product_id":"1","product_model":"HFJ5G1.5","product_type":"plat","product_return":"graviteits"},"2":{"product_id":"2","product_model":"HHJ5S2.5","product_type":"holle plunjer","product_return":"veer"}}

when i do alert(jsonString.length); the result is 4 But i want the result to be 2 am i doing something wrong .

  • 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-30T00:17:39+00:00Added an answer on May 30, 2026 at 12:17 am

    an object literal has no .length

    you can count properties using this method:

    var count = 0;
    
    for (i in jsonString) {
        if (jsonString.hasOwnProperty(i)) {
            count++;
        }
    }
    
    alert(count); //count shall have length for you
    

    OR

    since your array didn’t have numeric indices (starting from 0), it assumed you used an associative array, hence they dumped an object of items rather than an array of items.

    to turn them to numeric indices, all you have to do is use array_values before encoding them to json:

    json_encode(array_values($array));
    

    then the json will be an array.. then you can use length

    from this:

    Array(
    [1] => Array(
            [product_id] => 1
            [product_model] => HFJ5G1.5
            [product_type] => plat
            [product_return] => graviteits
        )
    [2] => Array(
            [product_id] => 2
            [product_model] => HHJ5S2.5
            [product_type] => holle plunjer
            [product_return] => veer       
        )
    );
    

    it becomes this using array_values(), note the indexes per item:

    Array(
    [0] => Array(
            [product_id] => 1
            [product_model] => HFJ5G1.5
            [product_type] => plat
            [product_return] => graviteits
        )
    [1] => Array(
            [product_id] => 2
            [product_model] => HHJ5S2.5
            [product_type] => holle plunjer
            [product_return] => veer       
        )
    );
    

    then encoded to json and stored to jsonString:

    jsonString = [
        {
            "product_id": "1",
            "product_model": "HFJ5G1.5",
            "product_type": "plat",
            "product_return": "graviteits"
        },
        {
            "product_id": "2",
            "product_model": "HHJ5S2.5",
            "product_type": "holle plunjer",
            "product_return": "veer"
        }
    ];
    
    alert(jsonString.length);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an multidimensional array that looks like this: Array ( [0] => Array
I have a multidimensional array like this: [ 'user 1' => [ ['item 4'
I have a multidimensional array that look like this: Array [1] => Array (
I have a multidimensional-array which looks like this: $array = ( [0] => array
I have a multidimensional array that looks something like this: ourThing = array( 'id'
I have a multidimensional array like this: array(2) { [1]=> array(3) { [eventID]=> string(1)
I have a multidimensional array that looks like this [0] => Array ( [recordId]
I have a php multidimensional array which looks like this: $fields = array( array('input',
say I have a multidimensional array like this: $data[0]['country'] = Angola $data[0]['report'] = Food
I have a multidimensional array like this : $products Smarty_Variable Object (3) ->value =

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.