Page 1 of 1

Array Structure -> Suggestions welcomed

Posted: Mon Dec 21, 2009 12:09 pm
by marty pain
I'm currently working on project that requires monthly accounting reports. I have just finished the first report, and would like some suggestions on the structure of the array used for the totals on the report.

The report shows what money has been received in a given time period and how that money was taken, cash, credit card, bank transfer etc. The report requires grand totals for the whole report and a break down on a month by month basis, both of which are further broken down by how the payment was taken.

Here is an example

Payment Type Totals
Cheque 75.00
Cash 250.00
Bank Transfer 100.00

Total Amount Received 425.00

September 2009
Cheque 75.00

Totals for September 2009 75.00

December 2009
Cash 250.00
Bank Transfer 100.00

Totals for December 2009 350.00


To achieve this I have created a multi-dimensional array to hold all the data necessary to produce the final totals.

It's current structure is as shown below :

Code: Select all

 
Array
(
    [totalReceived] => 425
    [payTypeHeads] => Array
        (
            [0] => Cheque
            [1] => Cash
            [2] => Bank Transfer
        )
 
    [payTypeTotals] => Array
        (
            [0] => 75
            [1] => 250
            [2] => 100
        )
 
    [monthTotals] => Array
        (
            [0] => Array
                (
                    [month] => September 2009
                    [1] => Array
                        (
                            [payType] => Cheque
                            [payTotal] => 75.00
                        )
 
                )
 
            [1] => Array
                (
                    [month] => December 2009
                    [1] => Array
                        (
                            [payType] => Cash
                            [payTotal] => 250
                        )
 
                    [2] => Array
                        (
                            [payType] => Bank Transfer
                            [payTotal] => 100.00
                        )
 
                )
 
        )
 
)
 
I was thinking of tidying it up, and changing it to this :

Code: Select all

 
Array
(
    [totalReceived] => 425
    [payTypeHeads] => Array
        (
            [Cheque] => 75
            [Cash] => 250
            [Bank Transfer] => 100
        )
 
    [monthTotals] => Array
        (
            [September 2009] => Array
                (
                      [Cheque] => 75.00
                )
 
            [December 2009] => Array
                (
                   
                      [Cash] => 250
                      [Bank Transfer] => 100.00
 
                )
      )
)
 
 
Please bare in mind that I DO want to print the array names as these will be used as headers.

Is this structure ok, or is there a better way of organising the array? If you have any suggestions please let me know.

I'm leaving it for the night now, look forward to your responses tomorrow.
Thanks!

Re: Array Structure -> Suggestions welcomed

Posted: Mon Dec 21, 2009 12:44 pm
by Christopher
You second design looks better than your first. Those are crying out to be classes with the top level array elements as properties and methods to abstract the operations (like totaling).

Re: Array Structure -> Suggestions welcomed

Posted: Tue Dec 22, 2009 4:12 am
by marty pain
Thanks!

I must admit that I didn't think about making it a class. There are around 20 reports all with very different totalling structures, so I guess I would need an abstract class for the totals with the abstract methods and then a class that implemented the totals needed by the report and the actual totalling for each report.

I'll think about it implementing it this way, however it wouldn't match the rest of the system design. My predecessor, who started the system, didn't exactly get the idea of OOP by the look of it. Although objects are utilised, it's far from being object orientated, and once you start polishing something it's hard to stop after you get that first shiny patch. And it's a big system :wink: