views:

257

answers:

2

I'm generating a multi-dimensional array in javascript that looks like this (this is the JSON representation of the javascript array, it's not in JSON format):

"100": {
 "40": {
    "subtotal": "24.99",
    "turn-around": {
        "0": "2-4 Business Days",
        "1": "Next Business Day (Add $15.00)"
    },
    "shipping": {
        "0": "UPS Ground - $0.00",
        "1": "UPS 2nd Day Air - $14.73",
        "2": "UPS 3 Day Select - $13.13"
    }
 },
 "41": {
    "subtotal": "29.99",
    "turn-around": {
        "0": "2-4 Business Days",
        "1": "Next Business Day (Add $15.00)"
    },
    "shipping": {
        "0": "UPS Ground - $0.00",
        "1": "UPS 2nd Day Air - $14.73",
        "2": "UPS 3 Day Select - $13.13"
    }
 }
}

I'm trying to convert this to JSON format, so I can import this to a PHP script. I'm using the JSON.stringify, but only getting the result:

[null,null,null,null,null,null,null,null,... CLIP... null,null,null,null,null,null,null,null,null,[]]]

I'm pretty sure the array's correct, because when dumping the contents, I get this:

'1000' ...
'41' ...
    'subtotal' => "$24.00"
    'tat' ...
        '0' => "- Choose Turnaround Time -"
        '1' => "Next Business Day (Add $15.00)"
        '2' => "2-4 Business Days"
    'shipping' ...
        '0' => "FREE UPS Ground - $0.00"
        '1' => "UPS 2nd Day Air - $12.75"
        '2' => "UPS 3 Day Select - $13.13"
        '3' => "UPS Next Day Air Saver - $15.32"
        '4' => "UPS Next Day Air - $17.04"
        '5' => "UPS Next Day Air Early A.M. - $71.61"

I'm not sure why the JSON.stringify method is not working. All I need it to get the array into a readable format to digest in PHP. Perhaps there's a better way?

All I need is to get a multi-dimensional array in javascript to a multi-dimensional array in PHP. I'm not a javascript expert, so that could be the real problem here.

+2  A: 

Since you are able to send the json data to php, you can do convert it using php too with its json_decode function.

Sarfraz
Yeah, you don't need to stringify your JSON (its *already* stringified, just send the contents of the object) what you need to do is parse and covert it into PHP's internal data structures. (json_decode)
indieinvader
Sorry, I should clarify that all I have right now is a javascript array (an object). I need to put this object into a JSON format. Or perhaps that's what I'm not understanding?I updated the post to try to clarify. My first code sample of the JSON is just to show the depth of the javascript array object.
drewjoh
JavaScript objects are already valid JSON documents, as long as your keys/values are in double-quotes. Even arrays are valid!
indieinvader
A: 

JSON stands for JavaSript Object Notation. which means that it can be used to carry any kind of data that can be represented using JavaScript's data structures (hashes and arrays) which means that these are all valid JSON objects:

foo = {
    "spam" : "eggs",
    "Yello" : [ "w", "Dello" ]
}
bar = [
    "Green",
    "Eggs",
    "Ham",
    { "Ron" : "Burgundy" }
]

So, if your array is already valid JSON there's no need to call stringify on it. And from what I can see, you're already good to go!

indieinvader