Sort an Array into arbitrary columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Sort an Array into arbitrary columns

Post by leenoble_uk »

Lets say I define a table as having 2 columns.
I have an array of 35 names which I want to display in alphabetical order such that:

Code: Select all

<tr><td>$array&#1111;0]</td><td>$array&#1111;18]</td></tr>
<tr><td>$array&#1111;1]</td><td>$array&#1111;19]</td></tr>
...
<tr><td>$array&#1111;17]</td><td>&nbsp;</td></tr>
Now I now how to display an arbitrary number of columns and I've written a function which will produce each row for a given number of columns and complete the last row with any necessary empty cells.
The difficulty I have is in sorting the array so that values appear in the right order.
After sorting alphabetically I need to break up the array into 2 columns like a card dealer would mix a deck of cards.
For future re-use purposes the function would do the same for any number of columns so if I changed the table layout from 2 to 3 cols then the function should make three lists when iterated through.
Seems like a good candidate for an already existing array function but I'm not certain if something exists and nothing screams at me as being obvious when looking through the manual.
I know I could break the information into 2 tables but then the rows wouldn't necessarily line up so this is not an option.
Well, is there a quick way of doing this with existing functions?
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

I can't be bothered to do any work today, so I thought i'd have a go at this!!!

I knocked together the following which seems to work... (haven't tested it properly)

Just call the function with an array and the number of columns your want...

Code: Select all

<?php
function table_array($array, $columns = "2") {

	$numcells = count($array);
	$lines = $numcells / $columns;
	if (!is_int($lines)) { $lines = intval($lines) + 1;}

	for ($i = 1; $i <= $lines; $i++) {
		$table .= '<tr>';
		$n = ($i - 1);
		for ($i2 = 1; $i2 <= $columns; $i2++) {
			$table .= '<td>'.$array[$n].'</td>';
			$n += $lines;
		}
		$table .= '</tr>';
	}

	$table = '<table border="1">'.$table.'</table>';

	return $table;
}

// ------------------------------------|

for ($i = 1; $i <= 48; $i++) {
	$array[] = 'Cell '.$i;
}
echo table_array($array, 5);?>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Take a look at array_chunk()
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post by leenoble_uk »

Cheers for the tip on array_chunk.
I found it just after I posted and was working with it but was having trouble with the pure maths of merging the multi-dimensional array back into a single array but then choppsta arrived with his code which I just checked out and works like a charm.
Maybe I'll come back to array_chunk some other time.
This is what I had so far - THIS IS NON WORKING CODE.

Code: Select all

<?php
$columns = 2;
	$perCol = ceil(count($array)/$columns);
	
	$array = array_chunk($input, $perCol);
	print_r($input);
	for($k=0; $k< count($input[0]); $k++)
	{
		foreach($array[$k] as $newKey=>$newVal)
		{
			$newArray[] = $newVal;
		}
	}
?>
Like I said, that's still a mess. I'll be using Choppsta's code for now but it would be nice to have a working function which works purely on the array and leaves the html out of it completely - at some stage.
Thanks.
Lee
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

How do you want your array arranged? This splits it into an array of rows for the table:

Code: Select all

$cols=4;
$diff=ceil(count($data)/$cols);
$out=array();
for ($c=0; $c<$diff; $c++) {
	$i=$c;
	$row=array();
	for ($d=0; $d<$cols; $d++) {
		$row[]=$data[$i];
		$i+=$diff;
	}
	$out[]=$row;
}
Post Reply