0

I have a javascript array like this (end result):

Array(
   [text_vars] => Array(
      [0] => 'xxxxxx',
      [1] => 'xxxxxx'
   )
)

Right now the loop I'm using to translate my JSON data into the array:

var aws = json.data;
var text_vars = new Array();

for(i=0; i < 4; i++){
   var id = aws[i]['id'];
   var name = aws[i]['name'];           
   text_vars[i] = id;
}

I then post the resulting array to my PHP processing page, I send them along with my jQuery post in this format:

{ text_vars: text_vars }

I need the array formatted like this:

Array(
   [text_vars][0] => 'xxxxx',
   [text_vars][1] => 'xxxxx'
)

The end goal is to prepare the data so it's ready to be posted via jQuery, like this:

{ text_vars[0]: 'xxxxx', text_vars[1]: 'xxxxx' }

So, if you can suggest a better method to transform the array into that format, I'm happy to hear it :)

1 Answer 1

2
var result = {}

for (var i = 0; i < text_vars.length; ++i) {
  result["text_vars[" + i + "]"] = text_vars[i]
}

Now result contains the structure you desire.

1
  • thanks dude! simple question but its been a while since I used js and it was driving me nuts! Commented Sep 27, 2011 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.