Access content of multi-dimensional array

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!
LVL 4
wolfet410Asked:
Who is Participating?
 
Aneesh ChopraConnect With a Mentor Commented:
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
 
PluckaCommented:
arrayListUsers[x][y]
0
 
wolfet410Author Commented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
PluckaCommented:
Are you using flex builder?

Put a break in and drill down on the variable and see what it looks like.
0
 
Aneesh ChopraCommented:
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
 
PluckaCommented:
@aneeshchopra

That wont change anything, arrayListUsers[0][0] should still exist, if infact this is an array.
0
 
wolfet410Author Commented:
Thanks for the code.
0
 
wolfet410Author Commented:
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
 
Aneesh ChopraCommented:
welcome,

as per your final comment, my suggestion at comment ID:22873415  would have worked for you if you would have tried it..
0
 
wolfet410Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.