如果两个值匹配,则组合/修改PHP数组

时间:2016-03-21 18:35:38

标签: php arrays for-loop multidimensional-array

如果两个键彼此匹配,我正在尝试组合两个数组并减去数组中的值。

这是我到目前为止编写的代码。

$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
    )
)

有什么建议吗?

4 个答案:

答案 0 :(得分: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
        )

)

答案 1 :(得分:0)

我对您的代码进行了一些更改,我认为我有您正在寻找的内容......

$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>";
}

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

答案 2 :(得分:0)

$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);

答案 3 :(得分:0)

要解决这样的问题,最好的方法是在脑海中或纸上解决问题,然后再实施。

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

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'])

希望有所帮助。