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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.