-1

I need to subtract the qt from two arrays based on id and type, keeping the array complete.

How do I do in php to be able to subtract these arrays?

I have 2 arrays:

=> this is the first array with multiple key "down"

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

=> this is the second array with multiple key "up"

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 need cubtract "qt" (if "id" and "loc" are the same then subtract the "qt")

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
        )
)
2
  • 1
    So what are you stuck on? Not seeing any code. Loop or maybe array_map? Commented Feb 23, 2022 at 7:44
  • Maybe try this. stackoverflow.com/questions/47160915/… A lot of similar answers on here. PHP array functions can do a lot, Commented Feb 23, 2022 at 7:53

1 Answer 1

0

Try this out

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.