Successfully Searching Multidimensional Arrays in AS3

July 15, 2010

Please be aware that this tutorial is intended for intermediate-to-advanced ActionScript programmers and designers. It is therefore assumed that you have a competent understanding of the underlying principles and specifics of both versions of ActionScript. I cannot respond to all questions regarding this code. So, please assume that you are on your own. Notes, instructions, and any relevant documentation has been included in the FLA files. Also, you will need Flash CS3 or CS4 to read the source files included in this tutorial.

The Problem
There are times when you are creating something in Adobe’s Flash content creation suite, that you want to be able to have an array of arrays. These are called multidimensional arrays, and they are great for many purposes. Their only big drawback seems to be the lack of searchability in the entire array and sub-array elements.

Multidimensional arrays are like a large red box, with medium-sized blue boxes inside and even smaller yellow boxes in each medium-sized blue box. So, you have the parent array (the red box) and its individual elements (the blue boxes). Each blue box then contains its own array (the yellow boxes).

I Just Can’t Find Anything!
Normally, when you need to find an occurance of some string in another string, you use the handy “indexOf” method like this:

var myIndex:Number = sourceString.indexOf( "my search target" );

And it will generate either “-1” (not found) or an integer which is equal to the target string’s starting position in the source string.

Then try using the “indexOf” method on a regular array and you also get the results you expect. But try the same method a multidimensional array and things just fall apart. You get nothing but the dreaded “-1” result: the string you were looking for was not found anywhere in the array. Even if you search in each array element individually, you must still look for only EXACT matches (===). You cannot find partial matches like you can when searching a standard string.


  • myArray.indexOf( “find this” ) = FAILS!
  • myArray[ element0 ].indexOf( “find this” ) = FAILS!
  • myArray[ element0 ][ element0 ].indexOf( “find this” ) = FAILS! (returns the starting locating in the array element string, but not the array index!)

Let there Be Light.
Ooooooh! Pretty colors… I have finally come up with a SIMPLE, quick solution to finding strings in multidimensional arrays. I do it by using the “forEach” method on an array. It passes the array element, index, and entire array object to a function which can then pick through the contents to look for partial matches and return an array on the array object that contains ALL of its instances in the parent array. If you read the results array, it is a set of row and column indexes that can be used directly on your parent array to display the contents of the elements that contain your search target string. It all sounds a bit confusing, I know it took me a while to get my head around the problem, but once you read and run the code below, it will become really clear just how simple and useful this code can be.

And Now, the Code, Please…
Click here to download the CS4/CS3 FLA file. You may use this code as part of any project as long as you do not sell my code on its own or as part of any large, commercial project. This code is copyright 2010 Clarence “exoboy” Bowman, all rights reserved.

Also, Here is the Code For you Copy-Pasters
// set up a multidimensional array that contains some data
var myArray:Array = new Array();
myArray.push(["granola","people... are great"," 4 ","10"]);
myArray.push(["steve","gates","24","yes, sometimes"]);

// here we set up some properties on the array object to hold our search string and our results
myArray.myTarget = "ste";
myArray.myResults = [];

// now we call the search

// this is the function that does all the heavy lifting....
function multiSearch(element:*, index:int, array:Array)
// see if we have a match in this array and pass back its index
for(var i:* in element)
if( element[i].indexOf( array.myTarget ) > -1 )
var tempArray:Array = array.myResults;
array.myResults = tempArray;

// -------------------------------------------------------------------------------
// all the code below is OPTIONAL... it is just to show our results
// in the output window in Flash so you know it worked....
var printArray:Array = myArray.myResults;
for(var i:* in printArray)
trace("TARGET FOUND @: "+printArray[i][0]+", "+printArray[i][1]+" = "+myArray[ printArray[i][0] ][ printArray[i][1] ]);
// -------------------------------------------------------------------------------