# Multi-dimensional arrays to 2-dimensional arrays

**URL:** <https://www.ruby-forum.com/t/multi-dimensional-arrays-to-2-dimensional-arrays/135857>\
**Category:** Ruby\
**Created:** [April 25, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/multi-dimensional-arrays-to-2-dimensional-arrays/135857 "2008-04-25T18:40:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wirianto\_Djunaidi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wirianto\_Djunaidi](https://www.ruby-forum.com/u/Wirianto_Djunaidi)\
**Post date:** [April 25, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/multi-dimensional-arrays-to-2-dimensional-arrays/135857/1 "2008-04-25T18:40:30Z")

</div>

Hi,

I have a recursive method that aggregates data in the form of  
2-dimensional  
arrays.  
The problem is at the top of the recursion call, I ended up with  
multi-dimensional arrays which depend  
on how deep the recursion is.

What is the best way to flatten multi-dimensional arrays back into  
2-dimensional arrays?  
the data might look like this:  
[[a, b, c], [[ [d, e], [f, g] ], [h, i] ]  
and I would like to be flatten into:  
[[a, b, c], [d, e], [f, g], [h, i] ]

Thanks in advance,  
-DJ

---

<div class="post-metadata">

**Author:** ![Wirianto\_Djunaidi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wirianto\_Djunaidi](https://www.ruby-forum.com/u/Wirianto_Djunaidi)\
**Post date:** [April 26, 2008, 5:55am UTC](https://www.ruby-forum.com/t/multi-dimensional-arrays-to-2-dimensional-arrays/135857/2 "2008-04-26T05:55:01Z")

</div>

Wirianto Djunaidi wrote:

> the data might look like this:  
> [[a, b, c], [[ [d, e], [f, g] ], [h, i] ]  
> and I would like to be flatten into:  
> [[a, b, c], [d, e], [f, g], [h, i] ]
> 
> Thanks in advance,  
> -DJ

I don’t think you’ve properly defined your inputs and outputs. You  
shouldn’t need an extra “flattening” step if your recursion is correctly  
defined.

---

<div class="post-metadata">

**Author:** ![Wirianto\_Djunaidi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wirianto\_Djunaidi](https://www.ruby-forum.com/u/Wirianto_Djunaidi)\
**Post date:** [April 28, 2008, 6:02am UTC](https://www.ruby-forum.com/t/multi-dimensional-arrays-to-2-dimensional-arrays/135857/3 "2008-04-28T06:02:06Z")

</div>

From: Wirianto Djunaidi [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I have a recursive method that aggregates data in the form of

# 2-dimensional arrays.

# The problem is at the top of the recursion call, I ended up with

# multi-dimensional arrays which depend

# on how deep the recursion is.

# What is the best way to flatten multi-dimensional arrays back into

# 2-dimensional arrays?

# the data might look like this:

# [[a, b, c], [[ [d, e], [f, g] ], [h, i] ]

# and I would like to be flatten into:

# [[a, b, c], [d, e], [f, g], [h, i] ]

you can also recurse it back 😉

eg,

a = [[:a, :b, :c], [[[:d, :e], [:f, :g]], [:h, :i]]]  
#=\> [[:a, :b, :c], [[[:d, :e], [:f, :g]], [:h, :i]]]

class Array  
def flat2  
a=[]  
self.each { |e| a += ( e == e.flatten ? [e] : e.flat2 ) }  
a  
end  
end  
#=\> nil

p a.flat2  
[[:a, :b, :c], [:d, :e], [:f, :g], [:h, :i]]  
#=\> nil

of course, the better way is to not perform this (back) recursing at all  
by reexamining your first recurs algo (as suggested by Ed B)

kind regards -botp

---

<div class="post-metadata">

**Author:** ![Wirianto\_Djunaidi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wirianto\_Djunaidi](https://www.ruby-forum.com/u/Wirianto_Djunaidi)\
**Post date:** [April 29, 2008, 9:32am UTC](https://www.ruby-forum.com/t/multi-dimensional-arrays-to-2-dimensional-arrays/135857/4 "2008-04-29T09:32:26Z")

</div>

Yeah, I figure out my problem. The reason I got nested multi-dimension  
array  
is each recursion return a 2-d array. And when I combined them together  
I  
use \<\<, which add the whole 2-d as 1 object into the result at the lower  
stack. I switch it to use += to combine the array and that fix it. 😛

Thanks all,  
-DJ
