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 7068381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:19:01+00:00 2026-05-28T05:19:01+00:00

I use JSON.stringify method to pass an array to server. I have an array

  • 0

I use JSON.stringify method to pass an array to server.

I have an array that has 4 elements:

arr[10] = 1;
arr[20] = 1;
arr[30] = 1;
arr[40] = 1;

Then i do this:

arr = JSON.stringify(arr);

Then send it to the server:

                jQuery.ajax({
                    type: "post",
                    url: baseurl+"profile/mprofile/action/ratings/add_ratings",
                    data:{"checkbox":checkbox,"review":review,"speciality":speciality,"arr":arr},
                    success: function(data, status) {  

                        jQuery('#header-error').html(data);
                    } 
                }); 

I am getting the array in PHP with:

    $arr = $this->ci->input->post('arr');
    $arr = json_decode($arr);
    print_r($arr) ; die ;

the result is

Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => 
    [10] => 1
    [11] => 
    [12] => 
    [13] => 
    [14] => 
    [15] => 
    [16] => 
    [17] => 
    [18] => 
    [19] => 
    [20] => 1
    [21] => 
    [22] => 
    [23] => 
    [24] => 
    [25] => 
    [26] => 
    [27] => 
    [28] => 
    [29] => 
    [30] => 1
    [31] => 
    [32] => 
    [33] => 
    [34] => 
    [35] => 
    [36] => 
    [37] => 
    [38] => 
    [39] => 
    [40] => 1
)

Why is this happening?

  • 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-28T05:19:01+00:00Added an answer on May 28, 2026 at 5:19 am

    That’s how javascript arrays work. When you set value to an index that is out of range, array is expanded, value is successfully set and all ‘missing’ values are set to undefined.

    You are spoiled by PHP behaviour that blends the difference between arrays and dictionaries (or “associative arrays”, in PHP talk).

    To avoid this behaviour, just create a dictionary in javascript instead of array.

    var arr = {};
    arr[10] = 1;
    arr[20] = 2;
    

    Also, on PHP side you should pass true as a second parameter to json_decode.

    json_decode($arr, true)
    

    This will make it return array instead of stdclass.

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

Sidebar

Related Questions

I have gathered data with jQuery, putten it into a multidimensional array, used JSON.stringify
I use json data with jquery... I have filepaths in json and i am
i'm trying to use json_decode to combine a few json objects and then re-encode
I am trying to use JSON.stringify() (from json2.js of json[dot]org ) to convert a
I just learned about jquery's .makeArray and I am trying to use JSON.stringify to
I have a few arrays that I want to send to process with PHP.
I can't use JSON.Stringify because the document object goes many many levels deep. I
I have been happy serializing with javascript objects into JSON using JSON.stringify And sending
In javascript, I want to use JSON.stringify . But some (older) browsers do not
I have an array of objects in javascript and I know that I need

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.