i've tried a few different json methods (stringify, toJSON, and probably some totally irrelevant others out of desperation) but can't seem to figure out how to stringify this so i can pass it to a php script. i am able to create a two dimensional array that which could be represented something like this:
array(
'image'=>array(
0=>'hello.jpeg',
1=>'goodbye.jpeg',
2=>'etc.jpeg'),
'resume'=>array(
0=>'resume.doc'),
'reel'=>array(
0=>'reel.mov')
)
the array looks okay when i print it to console using this dump function. i tried figuring out how to get this to work with objects because i thought i read something that said objects were already JSON friendly, but since i'm not super familiar with javascript i was pretty much floundering about.
edit: some more details... the declaration of my array (when i had it working) was something like this, although i may have messed up:
var fileArray = [];
fileArray['image'] = [];
fileArray['resume'] = [];
fileArray['reel'] = [];
var type;
var name;
var divs = $("#upload-container").children('div');
$.each(divs, function() {
type = $(this).attr('id');
name = $(this).html();
fileArray[type].push(name);
});
-
Are you sure you mean the outer structure to be an array?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2011年09月22日 23:12:59 +00:00Commented Sep 22, 2011 at 23:12
-
i think so. if the var was named fileArray, then each element would be something like fileArray['image'][0], fileArray['image'][1], fileArray['image'][2], fileArray['resume'][0], and fileArray['reel'][0].Justin– Justin2011年09月22日 23:16:18 +00:00Commented Sep 22, 2011 at 23:16
-
I ask because normally we'd use an object for that, not an array.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2011年09月22日 23:16:44 +00:00Commented Sep 22, 2011 at 23:16
-
yeah, part of the problem was declaring the object and mixing up syntax. the equivalent of what i want to do in php would be something like this: foreach($unimportant as $key => $var) { fileArray[$key][] = $var; }, where there are multiple 'vars' categorized under the 'key' branches.Justin– Justin2011年09月22日 23:22:37 +00:00Commented Sep 22, 2011 at 23:22
-
But... JavaScript is not PHP.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2011年09月22日 23:25:26 +00:00Commented Sep 22, 2011 at 23:25
2 Answers 2
The object for that array structure might look like this in JavaScript:
var objects =
[
{
'image': [
{ '0': 'hello.jpeg' },
{ '1': 'goodbye.jpeg' },
{ '2': 'etc.jpeg' }
]
},
{
'resume': [
{ '0': 'resume.doc' }
]
},
{
'reel': [
{ '0': 'reel.mov' }
]
}
]
So now you've got an array of three objects, each of which contains a property (image, resume, reel) that is another array of objects with basically key:value pairs ('0':'hello.jpeg'). Maybe you could simplify it by not bothering to use the indexes:
var objects =
[
{
'image': [ 'hello.jpeg', 'goodbye.jpeg', 'etc.jpeg' ]
},
{
'resume': [ 'resume.doc' ],
},
{
'reel': [ 'reel.mov' ]
}
]
Then you can use JSON.stringify(objects) to pass to your PHP action.
3 Comments
Your sample expected output on the PHP side has an associative array containing numeric arrays. JavaScript arrays have numeric indexes: if you want strings as keys use a plain object rather than an array because JavaScript objects act like the associative arrays you are thinking of from PHP. The corresponding JavaScript object should look like this:
var fileArray = {
'image' : [ 'hello.jpeg',
'goodbye.jpeg',
'etc.jpeg'],
'resume' : [ 'resume.doc' ],
'reel' : [ 'reel.mov' ]
};
In the arrays defined with square brackets the numeric indexes are implied. Note that fileArray is defined with curly braces not square brackets and so is not an array (in spite of its name). This still allows you to use the fileArray['image'] = ... syntax if you want to set the properties one at a time:
var fileArray = {}; // an object, not an array
fileArray['image'] = [];
fileArray['resume'] = [];
fileArray['reel'] = [];
Note that the curly brackets in the initial declaration make it an object but properties are still accessed with square bracket syntax.
The way you were defining fileArray as a JavaScript array with square brackets still allows you to add string-based key properties because arrays are objects, but JSON stringify routines may ignore those properties and only serialise the numerically indexed properties.
4 Comments
var fileArray = {}. Your next three lines add properties image, resume and reel each of which is an array. Within your .each loop fileArray[type] accesses one of those arrays, and fileArray[type].push(name); pushes the name value into the associated array.