Get all elements of a multidimensional array

I am looking for a way of returning a multidimensional array as a non-multidimensional array, as in converting this:

a:Array = [obj1, [obj2, [obj3, [obj4, [obj5, obj6]]]]]

to a “flat” array, like this:

var b:Array = foobar(a);
trace(b); // obj1,obj2,obj3,obj4,obj5,obj6
function foobar(arr:Array):Array {
     // what do I do with a?
}

Here a.length = 2, since it contains an object and an array, and b.length = 6. For my application, I need a function that will determine b for any dimensional depth of a. Because at some point the innermost array in a will change from [obj5,obj6] to [obj5,[obj6,obj7]]. Once this happens, of course a.length will still be 2 but b.length would be 7.

I have a feeling this involves a while or do while loop, but my brain has some type of aversion to those and I don’t seem to be able to ever formulate such a loop successfully.

Any help? Thanks!