Quote:
Originally Posted by jess
HELP ME SOMEONE PLEEEEASE!!! I need to create a multi-dimensional array...(?) - and thats all I know.... (well almost!)
|
Let's me introduce to you a good way with an example.
XML content:
Code:
<?xml version="1.0" standalone="yes"?>
<RECORDS>
<RECORD>
<ID>1</ID>
<AlbumId>1</AlbumId>
<SongId>1</SongId>
<CommentWriter>asdasd</CommentWriter>
<CommentReader>asdasdsddddd</CommentReader>
<RECORD>
<RECORDS>
Actionscript 2.0
Code:
function processXMLData(success)
{
if (success)
{
comments = this.firstChild.childNodes;
for (var i = 0; i < comments.length; i++){
albumId = comments[i].childNodes[1].childNodes; //AlbumId
songId = comments[i].childNodes[2].childNodes; //SongId
commentWriter = comments[i].childNodes[3].childNodes; //CommentWriter
commentReader = comments[i].childNodes[4].childNodes; //CommentReader
//Create the first array
var tMultiArray = new Array();
//Create the child array of fist array
var tMultiArrayChild = new Array();
//Initial value for the child array
tMultiArrayChild = ["AlbumId", albumId];
//Then push the child to fist array
tMultiArray.push(tMultiArrayChild);
//Do it again
tMultiArrayChild = ["SongId", songId];
tMultiArray.push(tMultiArrayChild);
//Do it again
tMultiArrayChild = ["CommentWriter", commentWriter];
tMultiArray.push(tMultiArrayChild);
//Do it again
tMultiArrayChild = ["CommentReader", commentReader];
tMultiArray.push(tMultiArrayChild);
//We need to collect all multiarray so then we can reuse it
commentInfo.push(tMultiArray);
}
//When all things are done, then just try to get all multiarray from commentInfo
for (var i = 0; i < commentInfo.length; i++){
trace(commentInfo[i][0][0] + "|" + commentInfo[i][0][1]);
trace(commentInfo[i][1][0] + "|" + commentInfo[i][1][1]);
trace(commentInfo[i][2][0] + "|" + commentInfo[i][2][1]);
trace(commentInfo[i][3][0] + "|" + commentInfo[i][3][1]);
}
}
else
{
content="Today's news is not found";
}
}
What is the result of code above? Take a look at this.
Code:
AlbumId|1
SongId|1
CommentWriter|asdasd
CommentReader|asdasdsddddd
AlbumId|1
SongId|2
CommentWriter|qwerqwrqwrqwr
CommentReader|erqwrqrqwrqwr
AlbumId|1
SongId|3
CommentWriter|werwerwer
CommentReader|werwerwer
AlbumId|1
SongId|3
CommentWriter|awerqrqwr
CommentReader|rqwrqwrqwr
AlbumId|2
SongId|1
CommentWriter|eqwrqwrqwr
CommentReader|qwrqwrqwrqwr
AlbumId|1
SongId|1
CommentWriter|asdasdasd12
CommentReader|asdasdasd1
AlbumId|1
SongId|1
CommentWriter|241234123
CommentReader|123123213
AlbumId|1
SongId|1
CommentWriter|241234123a
CommentReader|123123213
AlbumId|1
SongId|1
CommentWriter|asdasd
CommentReader|asddddd
I hope this can help.