dongxing1965 2016-10-25 02:35
浏览 23

循环中的PHP变量不会设置为0

So I have the following method:

public function generateTrialBalance( $period ) {
    $total_dr    = 0;
    $total_cr    = 0;
    $date        = NULL;

    $group_dr    = 0;
    $group_cr    = 0;

    $accountData = $this->find();

    $groupData = $this->findAllGroups();

    foreach( $groupData as $accountGroup ) {

        foreach( $accountData as $ledger ) {        

            $group = $this->findGroup( $ledger['group_id'] );   

            $ledgerFormat  = gl_format( $ledger['gl_account_main'], $ledger['gl_account_dept'], $ledger['gl_account_sub']);
            $ledgerEntries = $this->findLedgerEntries( $ledger['id'] );

            foreach( $ledgerEntries as $entries ) {
                $journalData = $this->findJournalEntry( $entries['entry_id'] );             
                $date        = date( 'm', $journalData['date'] );

                if ( $date <= $period ) {
                    $position = $entries['position'];
                    $amount   = $entries['amount'];
                    $position == "D" ? $total_dr += $amount : $total_cr += $amount; 
                }           
            }

            $total = $total_dr - $total_cr;

            $total > 0 ? $group_dr += $total : $group_cr +=$total;

            if( $accountGroup['id'] == $group['id']) {
                $this->ledgerTable[$group['name']][]    = array(
                        "account"           => $ledgerFormat,
                        "description"       => $ledger['name'],
                        "total"             => $total,
                        "group_dr"          => $group_dr,
                        "group_cr"          => $group_cr,
                );

            }
            $total_dr = 0;
            $total_cr = 0;
        }

        $group_dr = 0; // Here is where the issue needs resolving
        $group_cr = 0; // Resetting the variables to prepare for next iter
}

    return $this->ledgerTable;
}

To give an idea as to what some of my variables contain or do, $groupData has 9 rows. $accountData has about 33 rows

My goal is for every iteration of $groupData, find the rows in $accountData that match to $groupData and then append to a new array based on the findings. I get an array that looks like this:

array(
    "Group1" => array(
              array(
              "account"     => 'string';
              "description" => 'string';
              "total"       => 'float';
              "group_dr"    => 'float';
              "group_cr"    => 'float';
              ), 
               array(
              "account"     => 'string';
              "description" => 'string';
              "total"       => 'float';
              "group_dr"    => 'float';
              "group_cr"    => 'float';
             )
          ),
    "Group2" => array(
              array(
              "account"     => 'string';
              "description" => 'string';
              "total"       => 'float';
              "group_dr"    => 'float';
              "group_cr"    => 'float';
              ), 
               array(
              "account"     => 'string';
              "description" => 'string';
              "total"       => 'float';
              "group_dr"    => 'float';
              "group_cr"    => 'float';
             )
          ),

Everything works perfectly except for the critical moment when the first iteration of $groupData has finished, the $group_dr and $group_cr is set to 0 so as to prepare a new iteration for the next group. However, it doesn't seem to be set to zero. And I end up with this :

strangeness

Perhaps even weirder, if I remove the two variables at the bottom I end up with this:

evenweirder

Which makes me wonder if the variable is being cached somewhere else. But it isn't. It's just upsetting because the second loop has $total_dr and $total_cr with the exact same logic and resetting to zero without issue.

I feel like I've been overthinking and have tried several methods. If anyone could please explain where I am possibly going wrong because I know I am somewhere. Thank you

EDIT:

I am editing because there is another place where $group_dr and $group_cr is being edited and that is in:

$total > 0 ? $group_dr += $total : $group_cr +=$total;

However this shouldn't have an effect because this is in the second iteration. But I figured I would point it out

  • 写回答

1条回答 默认 最新

  • dqfsbvd43312 2016-10-25 03:36
    关注

    I figured out what's wrong, and I know I tried this solution somewhere down the line and it didn't seem to work:

    The line where $group_dr and $group_cr gets calculated in is the following:

    $total > 0 ? $group_dr += $total : $group_cr +=$total;
    

    However this is outside the if statement that checks the $groupData, so it completely ignores what group we're in ergo it's going to do the full iteration of $accountData and tally EVERYTHING up, which is why I keep getting those duplicates. However inserting this within the following if statement:

                if( $accountGroup['id'] == $group['id']) {
                $total > 0 ? $group_dr += $total : $group_cr +=$total; // it works
                    $this->ledgerTable[$group['name']][]    = array(
                            "account"           => $ledgerFormat,
                            "description"       => $ledger['name'],
                            "total"             => $total,
                            "group_dr"          => $group_dr,
                            "group_cr"          => $group_cr,
                    );
    

    Completely fixed the issue. I'm one-hundred percent that this was my FIRST solution, but something else must have happened that 'cause me to move things around.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大