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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:16:30+00:00 2026-06-07T03:16:30+00:00

Suppose I have the following data: var arr = [], arr1 = [], arr2

  • 0

Suppose I have the following data:

var arr = [], arr1 = [], arr2 = [], arr3 = [], arr4 = [];
var a = 'something', b = 'else';
arr1['key1-1'] = 'value1-2';
arr1['key1-2'] = 'value1-2';
for (var i = 0; i < someCond; i++) {
    arr = [];
    arr['key2-1'] = 'value2-1';
    arr['key2-2'] = 'value2-2';
    arr2.push(arr);
}

Now I need to pass the hole of it to a php script.

I packaged it into a single variable like so:

var postVar = {
    a: a,
    b: b,
    arr1: arr1,
    arr2: arr2
};

I’m using jQuery so I tried to post it like this:
1)

//Works fine for a and b, not for the arrays
$.post('ajax.php', postVar, function(response){});

and this:
2)

var postVar = JSON.stringify(postVar);
$.post('ajax.php', {json: postVar}, function(response){});

with the php file

$req = json_decode(stripslashes($_POST['json']), true);

which also doesn’t work.

How should I structure/format my data as to send it to PHP?

Thanks

EDIT:
Case 1:
console.log(postVar);
console.log(postVar)

PHP print_r($_POST) response:
Array
(
[a] => something
[b] => else
)

As you can see, there are no arrays (objects) on the php side.

Case 2:
When I add the following:


    postVar = JSON.stringify(postVar);
    console.log(postVar);

I get
{“a”:”something”,”b”:”else”,”arr1″:[],”arr2″:[[],[],[]]}
with console.log(postVar)

So that seems to be the problem in this case… right?

  • 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-06-07T03:16:33+00:00Added an answer on June 7, 2026 at 3:16 am

    As it turns out, although Arrays are Objects, JSON.stringify ignores non Array properties on Arrays. So I had to explicitly declare all variables as objects. Except for arr2 that is really used as an array.

    Here goes the full code:

    var arr = {}, arr1 = {}, arr2 = [];
    var a = 'something', b = 'else';
    arr1['key1-1'] = 'value1-2';
    arr1['key1-2'] = 'value1-2';
    for (var i = 0; i < 3; i++) {
        arr = {};
        arr['key2-1'] = 'value2-1';
        arr['key2-2'] = 'value2-2';
        arr2.push(arr);
    }
    
    var postVar = {
        a: a,
        b: b,
        arr1: arr1,
        arr2: arr2
    };
    
    
    postVar = JSON.stringify(postVar);
    $.post('ajax.php', {json: postVar}, function(response){});
    

    And on the PHP side:

    $req = json_decode($_POST['json'], true);
    print_r($req);
    

    Hope this helps others with the same problem.

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

Sidebar

Related Questions

Suppose I have the following code: ... var data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil]; NSLog.trace(@getdData:
Suppose I have the following data frame: Data1 X1 X2 1 15 1 2
Suppose we have the following table data: ID parent stage submitted 1 1 1
Suppose I have the following types of data: class Customer { String id; //
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
Consider the following data model: Suppose I have a table called SuperAwesomeData where each
Suppose I have the following html: var testString = ' <span id='id'> <div> <table>
Suppose I have following data in my game. I have developed game in cocos2d.
Suppose I have two tables Table1 and Table2 with the following data. Column1 Column2
Suppose we have the following code: class A: var = 0 a = A()

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.