Multi-dimensional array help

This is a discussion on Multi-dimensional array help within the PHP General forums, part of the PHP Programming Forums category; This array problem has been stumping my brain for a little while now... here's a sample of my data: $...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-27-2003
Aaron Gould
 
Posts: n/a
Default Multi-dimensional array help

This array problem has been stumping my brain for a little while now...
here's a sample of my data:

$purchases[001][200304] = array('regular'=>3, 'booked'=>4);
$purchases[002][200303] = array('regular'=>5, 'booked'=>1);
$purchases[002][200304] = array('regular'=>1, 'booked'=>0);
$purchases[002][200307] = array('regular'=>0, 'booked'=>2);
$purchases[003][200303] = array('regular'=>0, 'booked'=>4);
$purchases[003][200301] = array('regular'=>5, 'booked'=>0);
$purchases[004][200309] = array('regular'=>0, 'booked'=>2);

The first array element of $purchases is a Part Number (or "SKU"). The
second element of $purchases is the month/year for which the assigned data
("regular" and "booked" values) are set.

I need to take this data and place it into a table that has a row for each
part number, and 12 months worth of cells. An example of the desired chart
(apologies to the
non-fixed font viewers!):

SKU| Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
001| | | | 3/4 | | | | | | | |
---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
002| | | 5/1 | 1/0 | | | 0/2 | | | | |
---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
003| 5/0 | | 0/4 | | | | | | | | |
---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
004| | | | | | | | | 0/2 | | |

In this case, the table layout would be 4 rows and 12 cells.

In this sample data's case, there are three rows of Part Number "002". This
needs to be combined into one row. There will only be a maximum of
12 elements for each SKU (there will never be duplicated Month/Year values).

Can anyone lead me in the right direction?
Reply With Quote
  #2 (permalink)  
Old 10-27-2003
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Multi-dimensional array help

On Mon, 2003-10-27 at 12:44, Aaron Gould wrote:
> This array problem has been stumping my brain for a little while now...
> here's a sample of my data:
>
> $purchases[001][200304] = array('regular'=>3, 'booked'=>4);
> $purchases[002][200303] = array('regular'=>5, 'booked'=>1);
> $purchases[002][200304] = array('regular'=>1, 'booked'=>0);
> $purchases[002][200307] = array('regular'=>0, 'booked'=>2);
> $purchases[003][200303] = array('regular'=>0, 'booked'=>4);
> $purchases[003][200301] = array('regular'=>5, 'booked'=>0);
> $purchases[004][200309] = array('regular'=>0, 'booked'=>2);
>
> The first array element of $purchases is a Part Number (or "SKU"). The
> second element of $purchases is the month/year for which the assigned data
> ("regular" and "booked" values) are set.
>
> I need to take this data and place it into a table that has a row for each
> part number, and 12 months worth of cells. An example of the desired chart
> (apologies to the
> non-fixed font viewers!):
>
> SKU| Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
> ---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
> 001| | | | 3/4 | | | | | | | |
> ---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
> 002| | | 5/1 | 1/0 | | | 0/2 | | | | |
> ---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
> 003| 5/0 | | 0/4 | | | | | | | | |
> ---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
> 004| | | | | | | | | 0/2 | | |
>
> In this case, the table layout would be 4 rows and 12 cells.
>
> In this sample data's case, there are three rows of Part Number "002". This
> needs to be combined into one row. There will only be a maximum of
> 12 elements for each SKU (there will never be duplicated Month/Year values).
>
> Can anyone lead me in the right direction?


I was feeling generous so I whipped up the answer for you :)

<?php

$purchases[001][200304] = array('regular'=>3, 'booked'=>4);
$purchases[002][200303] = array('regular'=>5, 'booked'=>1);
$purchases[002][200304] = array('regular'=>1, 'booked'=>0);
$purchases[002][200307] = array('regular'=>0, 'booked'=>2);
$purchases[003][200303] = array('regular'=>0, 'booked'=>4);
$purchases[003][200301] = array('regular'=>5, 'booked'=>0);
$purchases[004][200309] = array('regular'=>0, 'booked'=>2);

echo '<table border="1">'."\n"
.'<tr>'
.'<th>SKU</th>'
.'<th>Jan</th>'
.'<th>Feb</th>'
.'<th>Mar</th>'
.'<th>Apr</th>'
.'<th>May</th>'
.'<th>Jun</th>'
.'<th>Jul</th>'
.'<th>Aug</th>'
.'<th>Sep</th>'
.'<th>Oct</th>'
.'<th>Nob</th>'
.'<th>Dec</th>'
.'</tr>'."\n";

foreach( $purchases as $sku => $skuData )
{
echo '<tr>'
.'<td>'.sprintf( '%03d', $sku ).'</td>';

for( $i = 1; $i <= 12; $i++ )
{
$key = '2003'.sprintf( '%02d', $i );

echo '<td>';

if( isset( $skuData[$key] ) )
{
echo $skuData[$key]['regular'].'/'.$skuData[$key]['booked'];
}
else
{
echo '&nbsp;';
}

echo '</td>';
}

echo '</tr>'."\n";
}

echo '</table>'."\n";


Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Reply With Quote
  #3 (permalink)  
Old 10-27-2003
David Otton
 
Posts: n/a
Default Re: [PHP] Multi-dimensional array help

On 27 Oct 2003 13:20:48 -0500, you wrote:


>echo '<table border="1">'."\n"
> .'<tr>'
> .'<th>SKU</th>'
> .'<th>Jan</th>'
> .'<th>Feb</th>'
> .'<th>Mar</th>'
> .'<th>Apr</th>'
> .'<th>May</th>'
> .'<th>Jun</th>'
> .'<th>Jul</th>'
> .'<th>Aug</th>'
> .'<th>Sep</th>'
> .'<th>Oct</th>'
> .'<th>Nob</th>'
> .'<th>Dec</th>'
> .'</tr>'."\n";


for ($i = 1; $i <= 12; $i++)
{
echo ('<th>' . strftime ("%b", strtotime ("1970-$i-01")) . '</th>');
}

is shorter, and stands a chance of working across locales.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:17 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0