/ Published in: ActionScript
I use this function in php a lot, and now I can use it in my Flash AS2 projects. Searches through an array for a string value and returns the index of that string. Can be modified to search for other data types ie. MovieClip, Number, etc...
Expand |
Embed | Plain Text
// function to search through an array to see if a value is in there. // Returns the index of the item in the array if found otherwise returns -1 // needle can be other data types, including MovieClip, Number, etc... function array_search(needle:String, haystack:Array):Number { var i:Number; var found:Boolean = false; for (i=0; i<haystack.length; i++) { if (haystack[i] == needle) { found = true; break; } } if (found == true) { return (i); } else { return (-1); } } function searchArray():Void{ var arrayIndex:Number = array_search(mc, inputArray); if (arrayIndex != -1) { // do something } else { //trace("Not In Array"); } }
You need to login to post a comment.
