簡體   English   中英

如何在PHP中減去兩個相同數組的值

[英]how to subtract value of two same array in php

我下面有兩個這樣的數組。

Array
(
    [0] => stdClass Object
        (
            [sply_ty] => INTRB2B
            [nil_amt] => 0
            [expt_amt] => 0
            [ngsup_amt] => 0
        )

    [1] => stdClass Object
        (
            [sply_ty] => INTRB2C
            [nil_amt] => 0
            [expt_amt] => 0
            [ngsup_amt] => 0
        )

    [2] => stdClass Object
        (
            [sply_ty] => INTRAB2B
            [nil_amt] => 76.77
            [expt_amt] => 38.39
            [ngsup_amt] => 33.01
        )

    [3] => stdClass Object
        (
            [sply_ty] => INTRAB2C
            [nil_amt] => 0
            [expt_amt] => 0
            [ngsup_amt] => 0
        )

)
Array
(
    [0] => stdClass Object
        (
            [sply_ty] => INTRB2B
            [nil_amt] => 0
            [expt_amt] => 0
            [ngsup_amt] => 0
        )

    [1] => stdClass Object
        (
            [sply_ty] => INTRB2C
            [nil_amt] => 0
            [expt_amt] => 0
            [ngsup_amt] => 0
        )

    [2] => stdClass Object
        (
            [sply_ty] => INTRAB2B
            [nil_amt] => 4
            [expt_amt] => 0
            [ngsup_amt] => 1
        )

)

在這里我想要一個最終數組,其中包含相減的供給類型的值。

Array
(
    [0] => stdClass Object
        (
            [sply_ty] => INTRB2B
            [nil_amt] => 0
            [expt_amt] => 0
            [ngsup_amt] => 0
        )

    [1] => stdClass Object
        (
            [sply_ty] => INTRB2C
            [nil_amt] => 0
            [expt_amt] => 0
            [ngsup_amt] => 0
        )

    [2] => stdClass Object
        (
            [sply_ty] => INTRAB2B
            [nil_amt] => 72.77
            [expt_amt] => 38.39
            [ngsup_amt] => 32.01
        )

    [3] => stdClass Object
        (
            [sply_ty] => INTRAB2C
            [nil_amt] => 0
            [expt_amt] => 0
            [ngsup_amt] => 0
        )

)

我如何在不使用太多foreach的情況下獲得此數組,或者可能正在使用mysql

最快的方法將在mysql中完成,例如:

SELECT 
a.sply_ty AS sply_ty,
(a.nil_amt - b.nil_amt) AS nil_amt,
(a.expt_amt - b.expt_amt) AS expt_amt,
(a.ngsup_amt - b.ngsup_amt) AS ngsup_amt
FROM source AS a
LEFT JOIN subtract AS b ON a.sply_ty = b.sply_ty
WHERE ...

為了使其更快,請使用foreign key加入subtract表。 如果可以更然后在一個匹配的記錄subtract表,你應該使用SUMGROUP BYsubtract表,例:

SELECT 
a.sply_ty AS sply_ty,
(a.nil_amt - SUM(b.nil_amt)) AS nil_amt,
(a.expt_amt - SUM(b.expt_amt)) AS expt_amt,
(a.ngsup_amt - SUM(b.ngsup_amt)) AS ngsup_amt
FROM source AS a
LEFT JOIN subtract AS b ON a.sply_ty = b.sply_ty
WHERE ...
GROUP BY a.sply_ty, a.mil_amt, a.expt_amt, a.ngsup_amt;

另一方面,如果必須在PHP中使用array functions而不是foreach ,則它應該更快,例如:

$source = array( ... );
$subtract = array( ... );

// apply function for every element of array with using $subtract
array_walk(
    $source,
    function (&$item) use ($subtract) {
        // find matching subtract items, return array
        $subItems = array_filter(
            $subtract,
            function ($s) use ($item) {
                return $s->sply_ty == $item->sply_ty;
            },
        );
        // if its empty, just do nothing
        if (empty($subItems)) {
            return;
        }
        // if not empty get first element as only exists that I suppose, 
        // if there will be more then one element to subtract use another loop to subtract every
        $subItem = current($subItems);
        $item->nil_amt -= $subItem->nil_amt;
        $item->expt_amt -= $subItem->expt_amt;
        $item->ngsup_amt -= $subItem->ngsup_amt;
    }
);

假定$ initial和$ substract為初始數組。

直截了當的解決方案只是從另一個中尋找密鑰。

foreach($substract as $sub) {
    foreach($initial as $index => $obj) {
        if ($obj->sply_ty == $sub->sply_ty) {
            $initial[$index]->nil_amt -= $sub->nil_amt;
            $initial[$index]->expt_amt -= $sub->expt_amt;
            $initial[$index]->ngsup_amt -= $sub->ngsup_amt;
        }
    }
}

這個循環偏離了最佳路線。 首先創建查找(甚至創建一次)

即:

$lookup = [];
foreach($initial as $index => $obj) {
    $lookup[$obj->sply_ty] = $obj;
}

foreach($substract as $sub) {
    if (isset($lookup[$sub->sply_ty])) {
        $lookup[$sub->sply_ty]->nil_amt -= $sub->nil_amt;
        $lookup[$sub->sply_ty]->expt_amt -= $sub->expt_amt;
        $lookup[$sub->sply_ty]->ngsup_amt -= $sub->ngsup_amt;
    }
}

$result = array_values($lookup);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM