PHP Array & Grid

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
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

PHP Array & Grid

Post by dimxasnewfrozen »

Code: Select all

$columns = array("C1", "C2");
$rows      = array("Row1", "Row2");
I would like to be able to get a result in a table/grid

C1 | C2
Row1 | Row1C1 | Row1C2
Row2 | Row2C1 | Row2C2

I am trying to join the arrays which isn't really working well.

Code: Select all

	$large_array = array();
	
	foreach($columns as $c) {
		foreach ($rows as $row)
			$large_array[] = array($c => $row);
	}
    
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Array & Grid

Post by requinix »

Well, you could just

Code: Select all

echo "<table><tr>";
echo "<th></th>"; // empty cell in the top-left corner
foreach ($columns as $col) {
    echo "<th>{$col}</th>";
}
echo "</tr>";
foreach ($rows as $row) {
    echo "<tr>";
    foreach ($columns as $col) {
        echo "<td>{$row} {$col}</td>";
    }
    echo "</tr>";
}
echo "</table>";
Post Reply