5 Replies - 13505 Views - Last Post: 16 July 2007 - 08:02 AM Rate Topic: -----

#1 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Flatten 2d array into 1d array

Posted 06 July 2007 - 12:09 PM

Howdy,

I have an array list of values. Now, each value contains a list of numbers.

What I want to do is take each list of numbers within each value and just paste them into a new, single dimensional, array.

Does anyone know how I can do this without tons and tons of loops?
Is This A Good Question/Topic? 0
  • +

Replies To: Flatten 2d array into 1d array

#2 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Flatten 2d array into 1d array

Posted 06 July 2007 - 01:16 PM

$source = array(array(1,2,3),array(4,5,6),array(7,8,9));
$target = array();

foreach ($source as $list) {
	$target = array_merge($target,$list);
}



Although if your arrays are quite large this may not be the most optimal way to do it. Using 2 nested loops isn't really such a bad solution, and it's a lot clearer what's going on:

foreach ($source as $list) {
	foreach ($list as $value) {
	$target[] = $value;
	}
}



--serializer

This post has been edited by serializer: 06 July 2007 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 12
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: Flatten 2d array into 1d array

Posted 06 July 2007 - 01:17 PM

just need one loop, and put it into a function, to return a 1d array. Simple.
Was This Post Helpful? 0
  • +
  • -

#4 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Flatten 2d array into 1d array

Posted 07 July 2007 - 01:33 PM

Doh! Somehow I though I was in the PHP forum. Edit seems to be disabled for my previous post?

The loop you need would use the AddRange method of collection classes:


List<int> target = new List<int>();

foreach (List<int> list in source) {
	target.AddRange(list);
}





--serializer
Was This Post Helpful? 0
  • +
  • -

#5 kcormier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-July 07

Re: Flatten 2d array into 1d array

Posted 09 July 2007 - 10:43 AM

is it a 2d jagged array or a 2d square array? if it's square, are there any empty spots that you have to filter out?
Was This Post Helpful? 0
  • +
  • -

#6 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: Flatten 2d array into 1d array

Posted 16 July 2007 - 08:02 AM

It is a 2d square array.

I have kinda gone back and forth between nested loops and alternatives. For the sake of readability I just ended up using nested loops. This call is only being performed once. I was afraid that I needed to call this loop once every 10ms, and that just would have been killer on my system.

Thanks so much for the help guys, great ideas. =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1