首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于2个值的PHP减法多维数组

基于2个值的PHP减法多维数组
EN

Stack Overflow用户
提问于 2022-02-23 07:27:31
回答 1查看 32关注 0票数 -1

我需要根据id和type从两个数组中减去qt,以保持数组的完整性。

我如何在php中能够减去这些数组?

我有两个数组:

=>这是第一个具有多键"down“的数组

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [id] => 26
            [loc] => 1
            [type] => down
            [qt] => 12
        )

    [1] => Array
        (
            [id] => 32
            [loc] => 1
            [type] => down
            [qt] => 34
        )

    [2] => Array
        (
            [id] => 26
            [loc] => 2
            [type] => down
            [qt] => 5
        )
        
    [3] => Array
        (
            [id] => 86
            [loc] => 3
            [type] => down
            [qt] => 45
        )
        
    [4] => Array
        (
            [id] => 23
            [loc] => 9
            [type] => down
            [qt] => 3
        )
    [5] => Array
        (
            [id] => 23
            [loc] => 3
            [type] => down
            [qt] => 99
        )
)

=> --这是第二个具有多键"up“的数组

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [id] => 26
            [loc] => 1
            [type] => up
            [qt] => 5
        )
    [1] => Array
        (
            [id] => 86
            [loc] => 3
            [type] => up
            [qt] => 27
        )
    [2] => Array
        (
            [id] => 23
            [loc] => 9
            [type] => up
            [qt] => 3
        )
)

=> i需要立方"qt“(如果"id”和"loc“相同,则减去"qt")

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [id] => 26
            [loc] => 1
            [type] => total
            [qt] => 7
        )

    [1] => Array
        (
            [id] => 32
            [loc] => 1
            [type] => total
            [qt] => 34
        )

    [2] => Array
        (
            [id] => 26
            [loc] => 2
            [type] => total
            [qt] => 5
        )
        
    [3] => Array
        (
            [id] => 86
            [loc] => 3
            [type] => total
            [qt] => 18
        )
        
    [4] => Array
        (
            [id] => 23
            [loc] => 9
            [type] => total
            [qt] => 0
        )
    [5] => Array
        (
            [id] => 23
            [loc] => 3
            [type] => down
            [qt] => 99
        )
)
EN

回答 1

Stack Overflow用户

发布于 2022-02-23 08:35:18

试试这个

代码语言:javascript
复制
function findMatch($array, $id, $loc)
{
    foreach ($array as $key => $item) {
        if ($item["id"] == $id and $item["loc"] == $loc) {
            return $key;
        }
    }
    return false;
}

$total = [];
foreach($down as $d) {
    $matched = findMatch($up, $d["id"], $d["loc"]);
    if($matched !== false){
        $total[] = array_replace($d, [
            "type" => "total",
            "qt" => ($d["qt"] - $up[$matched]["qt"])
        ]);
    } else {
        $total[] = array_replace($d, ["type" => "total"]);
    }
}
print_r($total);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71232811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档