1

I want to create a nested arrays. Here is my code, after I loaded xml.

 function readXML(event:Event):void
 {
 _data = new XML(event.target.data);
 for each (var usr in _data.item)
 {
 allUserbase.push({name: usr.@name,state: usr.@state, 
 complex:  usr.complex,image:usr.@image, link: usr.@link});
 for (var k:int = 0; k< allUserbase.length; k++){
 trace(k, allUserbase[k].complex);
 for (var t:int = 0; t< allUserbase[k].length; t++){
 trace(k,t, allUserbase[k][t]);
 // this part when i try to built 2d array doesnt work.. :(*/
 }

Here is my XML:

   <content>
   <item image="John.jpg" name="John" state = "New Jersey" >
   <complex fid = "0"> mg749</complex>  
   <complex fid = "1"> ks749</complex>  
   <complex fid = "2"> ks678</complex>      
   </item>    
   <item image="Smith.jpg" name="Smith" state = "California">  
   <complex fid = "0"> we649</complex>  
   <complex fid = "1"> sd449</complex>  
   <complex fid = "2"> df459</complex>  
   <complex fid = "3"> hj569</complex>      
   </item>  
   <item image="Smith.jpg" name="Mike" state = "New York">  
   <complex fid = "0"> 8794</complex>  
   <complex fid = "1"> 4384</complex>     
   </item> 
   </content>; 
3
  • Is this the third time you've asked the same question in 3 days? and not accepted any of the old answers? Commented Feb 25, 2011 at 20:51
  • Oh yeah, I knew I saw this question before. Commented Feb 25, 2011 at 20:55
  • @Mike and @Taurayi -yes, i did'nt got my answer and was desperate but thanks a lot for help.. Commented Feb 28, 2011 at 14:57

2 Answers 2

1

Is the following along the lines of what you want?:

var contentXml:XML =        
<content>
    <item image="John.jpg" name="John" state = "New Jersey" >
        <complex fid = "0"> mg749</complex>  
        <complex fid = "1"> ks749</complex>  
        <complex fid = "2"> ks678</complex>      
    </item>    
    <item image="Smith.jpg" name="Smith" state = "California">  
        <complex fid = "0"> we649</complex>  
        <complex fid = "1"> sd449</complex>  
        <complex fid = "2"> df459</complex>  
        <complex fid = "3"> hj569</complex>      
    </item>  
    <item image="Smith.jpg" name="Mike" state = "New York">  
        <complex fid = "0"> 8794</complex>  
        <complex fid = "1"> 4384</complex>     
    </item> 
</content>; 

var contentArray:Array = new Array();

for each(var item in contentXml.item)
{
    var itemArray:Array = new Array();
    itemArray.push(item.@image, item.@name, item.@state);

    contentArray.push(itemArray);

    for each(var complex in item.complex)
    {
        var complexArray:Array = new Array();
        complexArray.push(complex.@fid, complex);

        itemArray.push(complexArray);

    }// end for each

}// end for each

trace(contentXml.item[0].@image); // outputs: John.jpg
trace(contentArray[0][0]) // outputs: John.jpg

trace(contentXml.item[0].complex[0]); // outputs: mg749
trace(contentArray[0][3][1]) // outputs: mg749

[UPDATE]

You can also use a combination of Array objects and Dictionary objects like the following:

var contentArray:Array = new Array();

for each(var item in contentXml.item)
{
    var itemDictionary = new Dictionary();
    itemDictionary["image"] = item.@image;
    itemDictionary["name"] = item.@name;
    itemDictionary["state"] = item.@state;

    var complexArray:Array = new Array();
    itemDictionary["complex"] = complexArray;

    contentArray.push(itemDictionary);

    for each(var complex in item.complex)
    {
        var complexDictionary:Dictionary = new Dictionary();
        complexDictionary["fid"] = complex.@fid
        complexDictionary["value"] = complex;

        complexArray.push(complexDictionary);

    }// end for each

}// end for each

trace(contentXml.item[0].@image); // outputs: John.jpg
trace(contentArray[0]["image"]) // outputs: John.jpg

trace(contentXml.item[0].complex[0]); // outputs: mg749
trace(contentArray[0]["complex"][0]["value"]) // outputs: mg749
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Taurayi, thanks a lot for help..this is awsome.. i am just confused about one thing, and pardon me if i am asking something stupid, but how do I "trace(contentXml.item[0].complex[2]); // outputs: ks678 trace(contentArray[0][3][1]); // outputs: mg749 how do i access "ks678" from here"... thanks a lot again for help..
@Taurayi..i got this too thanks a lot..dictionary objects works very well..I have one more question though i have to create a search function for this and I am bot able to get it right for complex, following s the code I am using: function search(MouseEvent):void { for (var n:int = 0; n<allUserbase.length; n++) { for (var k:int = 0; k<allUserbase[n].length; k++) { if ((allUserbase[n][k].complex.toLowerCase() == search_complex.text.toLowerCase() || search_complex.text=="")) { trace("found"); } } } }
0

{name: usr.@name,state: usr.@state, complex: usr.complex,image:usr.@image, link: usr.@link} is not an Array, but an Object declaration, its elements can be accessed by allUserbase[k].complex or allUserbase[k].['complex'], they don't have a numeric index.

1 Comment

thnx for replying. How do i access compplex data by creating a 2d array lilke allUserbase[0][1]..is there a way to do so..

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.