?
Solved

Access content of multi-dimensional array

Posted on 2008-11-03
10
Medium Priority
?
2,839 Views
Last Modified: 2013-11-11
I have an array created from an XML file (from a PHP script), that I pull into an arrayCollection using an HTTPService.

The XML file looks like this, and I'd like to filter on the value of cn0.

<record>
<cn0>Joe User</cn0>
<company0>Yahoo, Inc</company0>
<samaccountname0>joeuser1</samaccountname0>
</record>
<record>
<cn0>Jane User</cn0>
<telephonenumber0>+1 (888) 555-0800</telephonenumber0>
<company0>Yahoo, Inc</company0>
<samaccountname0>janeuser1</samaccountname0>
<mail0>user.jane@yinc.com</mail0>
</record>
..... and so on

My array is created as:
private var arrayListUsers:ArrayCollection=new ArrayCollection();
arrayListUsers = event.result.records.record as ArrayCollection;

I plan to filter for a string on *one* column of this array, using these functions:
private function fnFilterListUsers():void {
// Filters ListUsers
  arrayListUsers.filterFunction = fnSearch;
  arrayListUsers.refresh();
}
         
private function fnSearch(item:Object):Boolean {
  var colNos:int = arrayListUsers.length;
  for(var i:int=0;i<colNos;i++){
    if(item[arrayListUsers[i]] != null && item[arrayListUsers[i]].toString().toLowerCase().indexOf(txtSearch.text.toLowerCase()) != -1){
      return true;
    }
  }
    return false;
  }

I'm afraid arrayListUsers[i] is the incorrect method because I'm not specifying any columns in the array.  If I trace arrayListUsers[i] I simply see [object Object] for each row in my array.

Basically, how do I reference a particular column in a multi-dimension array, when the data is pulled from an XML file and I am not specifying the column names in the HTTPService or any other function?

Thanks!
0
Comment
Question by:wolfet410
  • 4
  • 3
  • 3
10 Comments
 
LVL 18

Expert Comment

by:Plucka
ID: 22870829
arrayListUsers[x][y]
0
 
LVL 4

Author Comment

by:wolfet410
ID: 22870978
That's what I thought, but it doesn't work as I'd expect.

trace(arrayListUsers[0][0]); returns undefined
trace(arrayListUsers[1][1]); returns undefined

There should be data there.

In your example, does x = the column or the row?  Do columns start at 0 or 1?
0
 
LVL 18

Expert Comment

by:Plucka
ID: 22871315
Are you using flex builder?

Put a break in and drill down on the variable and see what it looks like.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 22873415
you should use "for..in"  loop to trace out object data

try this:

var total = arrayListUsers.length;
for(var i=0;i<total;i++)
{
      for(var n in arrayListUsers[i])
      {
            trace(arrayListUsers[i][n]);
      }
}
0
 
LVL 18

Expert Comment

by:Plucka
ID: 22873435
@aneeshchopra

That wont change anything, arrayListUsers[0][0] should still exist, if infact this is an array.
0
 
LVL 34

Accepted Solution

by:
Aneesh Chopra earned 375 total points
ID: 22873454
use following class to trace out any objectwith 'n' level of object/arrays .. just copy and save it as "ObjectDumper3.as" into your project.

and use as following:

trace(ObjectDumper3.toString(arrayListUsers));
package
{ 
	import flash.xml.XMLNode;
	
	
	public class ObjectDumper3
	{	
		private static var inProgress;
		private static var maxLineLength = 100;
		private static var indent = 0;
		
		
		public static function sayHelloWorld():void
		{
			trace("helloWorld");
		};
		
		public static function toString(obj)
		{
			inProgress = new Array();
			return realToString(obj);
		}
		
		private static function realToString(obj)
		{
			inProgress.push(obj);
			
			var t = typeof(obj);
			var result;
			
			if ((obj is XMLNode))
			{
				result = obj.toString();
			}
			else if (obj is Date)
			{
				result = obj.toString();
			}
			else if (t == "object")
			{
				var nameList = new Array();
				if (obj is Array)
				{
					result = "["; // "Array" + ":";
					for (var i = 0; i < obj.length; i++)
					{
						nameList.push(i);
					}
				}
				else
				{
					result = "{"; // "Object" + ":";
					for (var q in obj)
					{
						nameList.push(q);
					}
					nameList.sort();
				}
					
				var sep = "";
				for (var j = 0; j < nameList.length; j++)
				{
					var val = obj[nameList[j]];
					
					var show = true;
					
					if (show)
					{
						result += sep;
						if (!(obj is Array))
							result += nameList[j] + ": ";
						result +=
							realToString(val);
						sep = ", `";
					}
				}
				
				
				if (obj is Array)
					result += "]";
				else
					result += "}";
			}
			else if (t == "function")
			{
				result = "function";
			}
			else if (t == "string")
			{
				result = "\"" + obj + "\"";
			}
			else
			{
				result = String(obj);
			}
			
			if (result == "undefined") result = "-";
			inProgress.pop();
			return replaceAll(result, "`", (result.length < maxLineLength) ? "" : ("\n" + doIndent(indent)));
		}
		
		
		private static function replaceAll (str : String, from: String, to: String)
		{
			var chunks = str.split(from);
			var result = "";
			var sep = "";
			for (var i = 0; i < chunks.length; i++)
			{
				result += sep + chunks[i];
				sep = to;
			}
			return result;
		}
		
		private static  function doIndent(indent)
		{
			var result = "";
			for (var i = 0; i < indent; i ++)
			{
				result += "     ";
			}
			return result;
		}
	}
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:wolfet410
ID: 31512862
Thanks for the code.
0
 
LVL 4

Author Comment

by:wolfet410
ID: 22879103
The answer to the question was:

arrayListUsers[n]['cn0']

The ActionScript gave me the information I needed to figure it out.

Thanks to both of you for the help.
0
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 22879137
welcome,

as per your final comment, my suggestion at comment ID:22873415  would have worked for you if you would have tried it..
0
 
LVL 4

Author Comment

by:wolfet410
ID: 22879334
Yes, that does work as well.  In your example, n is "cn0" (or whatever).  Because I misunderstood what "var n in arrayListUsers[i]" did, I figured n would be a number.  Since I already tried arrayListUsers[0][0], I skipped that test and went to the ActionScript code instead.  

I will use both methods in the future to find out exactly what is being stored in an array.

Thx
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
Suggested Courses
Course of the Month17 days, 7 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question