【问题标题】:Combine/modify PHP arrays if two values match如果两个值匹配,则合并/修改 PHP 数组
【发布时间】:2016-07-08 10:23:04
【问题描述】:

如果两个键相互匹配,我正在尝试合并两个数组并在数组中减去一个值。

这是我迄今为止编写的代码。

$dat = array();
for ($i = 0; $i <= (count($cashdrawer_sales) - 1); $i++) {
$obj = $cashdrawer_sales[$i];
foreach ($cashdrawer_refund as $k=>$ref) {
    if ($obj['cash_drawer']==$ref['cash_drawer'] && $ref['department_id']==$obj['department_id']) {
        $cash_drawer_total = $obj['cash_drawer_sale_total'] - $ref['cash_drawer_refund_total'];
        $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total);
        array_push($dat,$arr);
    } else {
        $cash_drawer_total = $obj['cash_drawer_sale_total'];
        //  echo $cash_drawer_total."<br>";
        $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total);
        array_push($dat,$arr);
    }
}
}
print_r($dat);

这是我想要操作的 $cashdrawer_sales 和 $cashdrawer_refund 数组的示例。

cashdrawer_sales

Array
(
[0] => Array
    (
        [department_id] => 80000
        [cash_drawer] => 21112
        [cash_drawer_sale_total] => 64.00
    )

[1] => Array
    (
        [department_id] => 80000
        [cash_drawer] => 21117
        [cash_drawer_sale_total] => 15.00
    )

[2] => Array
    (
        [department_id] => 80000
        [cash_drawer] => No Cash Drawer
        [cash_drawer_sale_total] => 50.00
    )

[3] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21112
        [cash_drawer_sale_total] => 193.00
    )

[4] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21113
        [cash_drawer_sale_total] => 30.00
    )

[5] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21117
        [cash_drawer_sale_total] => 10.00
    )

[6] => Array
    (
        [department_id] => 50502
        [cash_drawer] => No Cash Drawer
        [cash_drawer_sale_total] => 80.00
    )

[7] => Array
    (
        [department_id] => No Department
        [cash_drawer] => 21112
        [cash_drawer_sale_total] => 50.00
    )

[8] => Array
    (
        [department_id] => No Department
        [cash_drawer] => No Cash Drawer
        [cash_drawer_sale_total] => 125.00
    )

)

cashdrawer_refund

Array
(
[0] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21112
        [cash_drawer_refund_total] => 103.00
    )

[1] => Array
    (
        [department_id] => No Department
        [cash_drawer] => No Cash Drawer
        [cash_drawer_refund_total] => 25.37
    )
)

我觉得我真的很接近解决方案,但我无法正确解决

当我运行此代码时,每个原始值都会得到两个金额。所以(据我所知)我正在满足我多次通过的条件。

Array
(
[0] => Array
    (
        [department_id] => 80000
        [cash_drawer] => 21112
        [cash_drawer_total] => 64.00
    )

[1] => Array
    (
        [department_id] => 80000
        [cash_drawer] => 21112
        [cash_drawer_total] => 64.00
    )

[2] => Array
    (
        [department_id] => 80000
        [cash_drawer] => 21117
        [cash_drawer_total] => 15.00
    )

[3] => Array
    (
        [department_id] => 80000
        [cash_drawer] => 21117
        [cash_drawer_total] => 15.00
    )

[4] => Array
    (
        [department_id] => 80000
        [cash_drawer] => No Cash Drawer
        [cash_drawer_total] => 50.00
    )

[5] => Array
    (
        [department_id] => 80000
        [cash_drawer] => No Cash Drawer
        [cash_drawer_total] => 50.00
    )

[6] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21112
        [cash_drawer_total] => 90
    )

[7] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21112
        [cash_drawer_total] => 193.00
    )

[8] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21113
        [cash_drawer_total] => 30.00
    )

[9] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21113
        [cash_drawer_total] => 30.00
    )

[10] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21117
        [cash_drawer_total] => 10.00
    )

[11] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21117
        [cash_drawer_total] => 10.00
    )

[12] => Array
    (
        [department_id] => 50502
        [cash_drawer] => No Cash Drawer
        [cash_drawer_total] => 80.00
    )

[13] => Array
    (
        [department_id] => 50502
        [cash_drawer] => No Cash Drawer
        [cash_drawer_total] => 80.00
    )

[14] => Array
    (
        [department_id] => No Department
        [cash_drawer] => 21112
        [cash_drawer_total] => 50.00
    )

[15] => Array
    (
        [department_id] => No Department
        [cash_drawer] => 21112
        [cash_drawer_total] => 50.00
    )

[16] => Array
    (
        [department_id] => No Department
        [cash_drawer] => No Cash Drawer
        [cash_drawer_total] => 125.00
    )

[17] => Array
    (
        [department_id] => No Department
        [cash_drawer] => No Cash Drawer
        [cash_drawer_total] => 99.63
    )

)

我假设这种情况正在发生,因为我对每次销售进行了两次迭代以分析退款。但是,我不确定该怎么做。

这是我的终极目标:

Array
(
[0] => Array
    (
        [department_id] => 80000
        [cash_drawer] => 21112
        [cash_drawer_total] => 64.00
    )

[1] => Array
    (
        [department_id] => 80000
        [cash_drawer] => 21117
        [cash_drawer_total] => 15.00
    )


[2] => Array
    (
        [department_id] => 80000
        [cash_drawer] => No Cash Drawer
        [cash_drawer_total] => 50.00
    )


[3] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21112
        [cash_drawer_total] => 90
    )


[4] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21113
        [cash_drawer_total] => 30.00
    )


[5] => Array
    (
        [department_id] => 50502
        [cash_drawer] => 21117
        [cash_drawer_total] => 10.00
    )

[6] => Array
    (
        [department_id] => 50502
        [cash_drawer] => No Cash Drawer
        [cash_drawer_total] => 80.00
    )


[7] => Array
    (
        [department_id] => No Department
        [cash_drawer] => 21112
        [cash_drawer_total] => 50.00
    )

[8] => Array
    (
        [department_id] => No Department
        [cash_drawer] => No Cash Drawer
        [cash_drawer_total] => 99.63
    )
)

有什么建议吗?

【问题讨论】:

  • 你想为每个cash_drawer分开销售和退款吗?
  • 我想从相应的销售总额中减去退款总额,并有一个包含新总额的新数组。
  • 好的,但是您是否尝试为每个 cash_drawer 执行此工作?因为您的销售数组有 7 个元素,而您的退款数组有 2 个元素。
  • 是的,我正在尝试按 department_id 为每个现金抽屉执行此操作(cash_drawer 不是唯一的,deptartment_id 是)。
  • 这个想法是销售总额可能没有退款,但如果确实有退款,我需要从相应的总额中减去它并创建我的新数组。

标签: php arrays for-loop multidimensional-array


【解决方案1】:

使用department_idcash_drawer 的组合来创建结果键:

$result = array();

foreach( $cashdrawer_sales as $row )
{
    $key = $row['department_id'].'|'.$row['cash_drawer'];                      # <------
    if( !isset( $result[$key] ) )
    {
        $result[$key] = $row;
        unset( $result[$key]['cash_drawer_sale_total'] );
        $result[$key]['cash_drawer_total'] = 0;
    }
    $result[$key]['cash_drawer_total'] += $row['cash_drawer_sale_total'];
}

foreach( $cashdrawer_refund as $row )
{
    $key = $row['department_id'].'|'.$row['cash_drawer'];                      # <------
    if( !isset( $result[$key] ) )
    {
        $result[$key] = $row;
        unset( $result[$key]['cash_drawer_refund_total'] );
        $result[$key]['cash_drawer_total'] = 0;
    }
    $result[$key]['cash_drawer_total'] -= $row['cash_drawer_refund_total'];
}

然后使用array_values()删除键:

$result = array_values($result);

现在是$result

Array
(
    [0] => Array
        (
            [department_id] => 80000
            [cash_drawer] => 21112
            [cash_drawer_total] => 64
        )

    [1] => Array
        (
            [department_id] => 80000
            [cash_drawer] => 21117
            [cash_drawer_total] => 15
        )

    [2] => Array
        (
            [department_id] => 80000
            [cash_drawer] => No Cash Drawer
            [cash_drawer_total] => 50
        )

    [3] => Array
        (
            [department_id] => 50502
            [cash_drawer] => 21112
            [cash_drawer_total] => 90
        )

    [4] => Array
        (
            [department_id] => 50502
            [cash_drawer] => 21113
            [cash_drawer_total] => 30
        )

    [5] => Array
        (
            [department_id] => 50502
            [cash_drawer] => 21117
            [cash_drawer_total] => 10
        )

    [6] => Array
        (
            [department_id] => 50502
            [cash_drawer] => No Cash Drawer
            [cash_drawer_total] => 80
        )

    [7] => Array
        (
            [department_id] => No Department
            [cash_drawer] => 21112
            [cash_drawer_total] => 50
        )

    [8] => Array
        (
            [department_id] => No Department
            [cash_drawer] => No Cash Drawer
            [cash_drawer_total] => 99.63
        )

)

【讨论】:

  • 谢谢你。这是一个我自己不会想到的解决方案,而且比我尝试的更合乎逻辑。
【解决方案2】:

我对您的代码进行了一些更改,我想我已经找到了您想要的东西...

$dat = array();
for ($i = 0; $i < (count($cashdrawer_sales)); $i++) {
    $obj = $cashdrawer_sales[$i];
    foreach ($cashdrawer_refund as $k=>$ref) {
        if ($obj['cash_drawer']==$ref['cash_drawer'] && $ref['department_id']==$obj['department_id']) {
            $cash_drawer_total = $obj['cash_drawer_sale_total'] - $ref['cash_drawer_refund_total'];
            $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total);
            $dat[] = $arr;
        } else {
            if (!in_array($obj, $dat)) {  // <-- check the array for the item, if it's not there then add it
                $dat[] = $obj;
            }
        }
    }
}

foreach($dat as $test){
    print_r($test);
    echo "<br>";
}

基本上,在将当前项添加回数组之前,我会检查它是否已经存在。

【讨论】:

    【解决方案3】:
    $dat = array();
    for ($i = 0; $i <= (count($cashdrawer_sales) - 1); $i++) {
    
    
        $obj = $cashdrawer_sales[$i];
        $match=false; // assume initially that this $obj does not match any $ref till now
        foreach ($cashdrawer_refund as $k=>$ref) {
        //check if this ref matches if yes then break//you are for all obj each time it mathches as well as not matches an obj
             if ($obj['cash_drawer']==$ref['cash_drawer'] && $ref['department_id']==$obj['department_id']) {
                  $match=true;
                  break;    
             } 
         }
         // this accounts for one obj only once in your output
         if($match){
             $cash_drawer_total = $obj['cash_drawer_sale_total'] - $ref['cash_drawer_refund_total'];
             $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total);
             array_push($dat,$arr);
         }else{
             $cash_drawer_total = $obj['cash_drawer_sale_total'];
                 //  echo $cash_drawer_total."<br>";
             $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total);
             array_push($dat,$arr);
         }
    }
    print_r($dat);
    

    【讨论】:

    • 我还要补充一点,这不是最快的方法。您可以对数组 1 和数组 2 进行排序,然后使用更好的检查在 O(n logn) 而不是 O(n^2) 中完成此操作
    【解决方案4】:

    要解决这样的问题,最好的方法是在你的脑海中或在论文中解决它然后实施它。

    据我了解,您想为有退款的人修改销售总额。然后你需要检查你所有的销售额:

    foreach ($cashdrawer_sales as $sale_key => $sale) {
    }
    

    然后我们想检查这个部门的现金抽屉是否有退款。我们检查所有退款:

    foreach ($cashdrawer_sales as $sale_key => $sale) {
        foreach ($cashdrawer_refund as $refund_key => $refund) {
            if ($sale['cash_drawer'] === $refund['cash_drawer'] && 
                    $sale['department_id'] === $refund['department_id']) {
                // and if there was refund then we need to subtract it:
                $cashdrawer_sales[$sale_key]['new_total'] = $sale['cash_drawer_sale_total'] -
                        $refund['cash_drawer_refund_total'];
            }
        }
    }
    

    这应该可以完成工作。不是说要在同一个数组中提交更改,我使用了密钥($cashdrawer_sales[$sale_key]['new_total'])

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-02
      • 2020-05-09
      • 2018-04-07
      • 1970-01-01
      • 2017-11-26
      • 2017-02-07
      • 1970-01-01
      • 2017-02-11
      相关资源
      最近更新 更多