Transpose table from vertical to horizontal parsing using CodeIgniter

I have a table formatted like this:

Year             Rate
2008      48
2009      32
2010      54
2011      32
2012      40
2013      41

I need to transpose this table so that it is formatted like this:

         2008  2009  2010 2011  2012  2013
Rate    48          32       54     32      40      41

What would be the best way to accomplish this?  Thanks.
DanielAttardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
<?php // RAY_temp_danielattard.php
error_reporting(E_ALL);
echo '<pre>';

$matrix = array
( 'Year' => 'Rate'
, '2008' => '48'
, '2009' => '32'
, '2010' => '54'
, '2011' => '32'
, '2012' => '40'
, '2013' => '41'
)
;

// SHOW THE ORIGINAL MATRIX
foreach ($matrix as $key => $value)
{
    echo PHP_EOL . "$key $value";
}

// MAKE TWO ARRAYS
$years = array();
$rates = array();
foreach ($matrix as $key => $value)
{
    $years[] = $key;
    $rates[] = $value;
}

// CONSTRUCT THE DISPLAY
$yrow = '<tr>';
$rrow = '<tr>';
foreach ($years as $key => $value)
{
    $yrow .= '<td>' . $years[$key] . '</td>';
    $rrow .= '<td>' . $rates[$key] . '</td>';
}
$yrow .= '</tr>';
$rrow .= '</tr>';

// SHOW THE NEW MATRIX
echo '<table>'  . PHP_EOL;
echo $yrow      . PHP_EOL;
echo $rrow      . PHP_EOL;
echo '</table>';

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DanielAttardAuthor Commented:
Amazing work Ray.  Works like a charm.  Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.