1

有两个数组

第一个数组

Array
(
[0] => Array
    (
        [SUM(j.Amount)] => 
        [DebitAccount] => 
    )

[1] => Array
    (
        [SUM(j.Amount)] => 15842.88
        [DebitAccount] => 2310
    )

[2] => Array
    (
        [SUM(j.Amount)] => 3656.68
        [DebitAccount] => 2380
    )

[3] => Array
    (
        [SUM(j.Amount)] => 12178.20
        [DebitAccount] => 2610
    )
 )

第二个数组

Array
(
[0] => Array
    (
        [SUM(j.Amount)] => 
        [CreditAccount] => 
    )

[1] => Array
    (
        [SUM(j.Amount)] => 15842.88
        [CreditAccount] => 2310
    )

[2] => Array
    (
        [SUM(j.Amount)] => 30916.44
        [CreditAccount] => 2380
    )

[3] => Array
    (
        [SUM(j.Amount)] => 3133.70
        [CreditAccount] => 2620
    )
)

什么是目标?需要得到这样的输出

帐号为 2310,值为 0.00 (15842.88 - 15842.88)

帐号为 2380,值为 -30916.40 (3656.68 - 30916.44)

帐号是 2610,值是 12178.20 (12178.20 (DebitAccount) - 0.00 (CreditAccount))

帐号为 2620,值为 -3133.70 (0.00 (DebitAccount) - 3133.70 (CreditAccount))

现在试图了解如何做这样的事情if(DebitAccount == CreditAccount){from (corresponding DebitAccount SUM(j.Amount)) deduct (corresponding CreditAccount SUM(j.Amount));}

你能建议如何达到目标吗?

4

2 回答 2

1

处理具有不同数量成员的数组需要这样的东西(未经测试):-

<?php

usort($DAs, "cmpDA");
usort($CAs, "cmpCA");

$DaAccount = 0;
$CaAccount = 0;
$DaKey = 0;
$CaKey = 0;

while($DaAccount < 999999 OR $CaAccount < 999999)
{
    switch true
    {
        case $DAs[$DaKey]['DebitAccount'] == $CAs[$CaKey]['CreditAccount'] :
            echo "Account number is ".$DAs[$DaKey]['DebitAccount'].", Value is ".($CAs[$CaKey]['SUM(j.Amount)'] - $DAs[$DaKey]['SUM(j.Amount)'])." (".$CAs[$CaKey]['SUM(j.Amount)']." - ".$DAs[$DaKey]['SUM(j.Amount)'].")<br />";
            $DaKey = (($DaKey < count($DAs)) ? $DaKey + 1 : 999999);
            $CaKey = (($CaKey < count($DAs)) ? $CaKey + 1 : 999999);
            break;
        case $DAs[$DaKey]['DebitAccount'] < $CAs[$CaKey]['CreditAccount'] :
            echo "Account number is ".$DAs[$DaKey]['DebitAccount'].", Value is ".(0 - $DAs[$DaKey]['SUM(j.Amount)'])." (0 - ".$DAs[$DaKey]['SUM(j.Amount)'].")<br />";
            $DaKey = (($DaKey < count($DAs)) ? $DaKey + 1 : 999999);
            break;
        case $DAs[$DaKey]['DebitAccount'] > $CAs[$CaKey]['CreditAccount'] :
            echo "Account number is ".$CAs[$CaKey]['CreditAccount'].", Value is ".($CAs[$CaKey]['SUM(j.Amount)'] - 0)." (".$CAs[$CaKey]['SUM(j.Amount)']." - 0)<br />";
            $CaKey = (($CaKey < count($DAs)) ? $CaKey + 1 : 999999);
            break;
    }
}

function cmpDA($a, $b)
{
    return (($a['DebitAccount'] == $b['DebitAccount']) ? 0 : (($a['DebitAccount'] < $b['DebitAccount']) ? -1 : 1) );
}

function cmpCA($a, $b)
{
    return (($a['CreditAccount'] == $b['CreditAccount']) ? 0 : (($a['CreditAccount'] < $b['CreditAccount']) ? -1 : 1) );
}

?>

将 2 个数组排序为相同的键顺序。做一个while循环,而任一数组键都没有被最大化。当有键匹配时,您可以进行计算。当一个比另一个大时,另一个没有匹配的记录,所以把它放出一个增量数组键到另一个数组。如果它是数组的末尾,则将数组键设置为愚蠢而大的东西。最终,两个数组都将完成,并且两个键都设置为较高的值。

但是,如果数据存储在数据库中,那么在 SQL 中执行此操作可能会更快、更容易。

于 2013-07-01T11:03:18.417 回答
1

假设第一个数组是$DAs,第二个是$CAs

<?php
foreach($DAs as $i=>$da){
    foreach($CAs as $j=>$ca){
        if($da['DebitAccount'] == $ca['CreditAccount']){
            //output
            echo 'Account number is '.$da['DebitAccount'].', Value is '.$da['SUM(j.Amount)']-$ca['SUM(j.Amount)'].' ('.$da['SUM(j.Amount)'].' - '.$ca['SUM(j.Amount)'].')';
            //deduct the credit
            $DAs[$i]['SUM(j.Amount)'] -= $ca['SUM(j.Amount)'];
            //we need to use $DAs[$i] to be able to modify its value, because foreach can only give a copy of it.
        }
    }
}
?>
于 2013-07-01T08:59:59.533 回答