Jump to content

[SOLVED] Multi Dimension Array Sub total


Jezza22

Recommended Posts

I have a function that creates an Array to be used in JavaScript. The data supplied to the function is a php Array. The function works well, but I am trying to get a subtotal of key 3 where Key 0 is the same....

 

The php array looks as follows

 


Array
(
    [0] => Array
        (
            [0] => 7001
            [1] => CASH
            [2] => SL_Receipts
            [3] => -431.25
            [4] => 23/08/2009
            [5] => JS
        )

    [1] => Array
        (
            [0] => 7001
            [1] => CASH
            [2] => SL_Receipts
            [3] => -431.25
            [4] => 23/08/2009
            [5] => JS
        )

    [2] => Array
        (
            [0] => 7001
            [1] => CASH
            [2] => SL_Receipts
            [3] => -431.25
            [4] => 23/08/2009
            [5] => JS
        )

    [3] => Array
        (
            [0] => 7001
            [1] => BANK
            [2] => CASH
            [3] => -431.25
            [4] => 23/08/2009
            [5] => JS
        )

    [4] => Array
        (
            [0] => 7001
            [1] => CASH 
            [2] => SL_Receipts
            [3] => -431.25
            [4] => 23/08/2009
            [5] => JS
        )
    [5] => Array
        (
            [0] => 7003
            [1] => CHEQUES 
            [2] => CHQ 12345
            [3] => -5000.00
            [4] => 23/08/2009
            [5] => JS
        )
  [6] => Array
        (
            [0] => 7003
            [1] => CHEQUES 
            [2] => CHQ 123456
            [3] => -2000.00
            [4] => 23/08/2009
            [5] => JS
        )
)

 

 

Here is my attempt that is not working because, It fails to total all of key 3 and only sub totals once..

 

Please Help !!

 

 

while ($array = mysql_fetch_array($result, MYSQL_NUM))
{$data[] = $array;
}

//echo '<pre>' . print_r($data, true) . '</pre>';

sort($data);

function displayArrayData($arrayValues)
{

	echo "[";	
	$lastRowID = '';

    //Iterrate through each record
    foreach ($arrayValues as $dataRow)
    {



         //Show blank row if this record is different ID than last
        if($dataRow[0]!=$lastRowID && $lastRowID != '')
        {
            
		if ($_GET[breakDown]=="YES")
			{
				echo '[],';
				echo '["","","Total",'.$dataRow[3].'],';
				echo '[],';
		     }

			 else

			 {

			 }
        }  
        //Set last ID value
        $lastRowID = $dataRow[0];


	//Display current record
        echo '    [';
        
	for ($a = 0; $a <= 8; $a++) 
	{
		echo '    "'.$dataRow[$a].'",';

	}
	echo '],';
}
    //Close the table
    echo '];';
}



 

 

This displays the following...

 

[ 
[ "7001", "CASH", "SL_Receipts", "-431.25", "23/08/2009", "JS", "", "", "",], 
[ "7001", "CASH", "SL_Receipts", "-431.25", "23/08/2009", "JS", "", "", "",], 
[ "7001", "CASH", "SL_Receipts", "-431.25", "23/08/2009", "JS", "", "", "",], 
[ "7001", "CASH", "SL_Receipts", "-431.25", "23/08/2009", "JS", "", "", "",], 
[],
["","","Total",-5000.00],
[], 
[ "7003", "CHEQUES", "Chq 12345", "-5000.00", "24/08/2009", "JS", "", "", "",], 
[ "7003", "CHEQUES", "Chq 123456", "-2000.00", "15/08/2009", "JS", "", "", "",],
];

 

 

Link to comment
https://forums.phpfreaks.com/topic/174254-solved-multi-dimension-array-sub-total/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.