1

I have two arrays that look like this:

$r1[$db_rate_type] = Array($db_rate_in, $db_rate_out, $db_rate_desc);
$r2[$db_rate_type] = Array($db_rate_in, $db_rate_out, $db_rate_desc);

Lets say the value of each array looks like this:

Array 1:
[1] = [1400] [20] ["Standard timelønn"]
[2] = [NULL] [20] ["Kveldstillegg"]

Array 2:
[1] = [NULL] [20] ["Standard timelønn"]
[2] = [1500] [20] ["Kveldstillegg"]

How can I sum the value of the array, but keep the counting IDs at the beginning?

I have tried the following code:

$c = array_map(function () {
    return array_sum(func_get_args());
}, $a, $b);

But it counts only for one array added to another array, and not arrays WITHIN arrays. What I prefer to have returned is the following:

Array returned:
[1] = [1400] [40] ["Standard timelønn"]
[2] = [1500] [40] ["Kveldstillegg"]

So how can I do this, but have it as effective as possible?

Full code of the database fetch etc. can be found here

2
  • What version of PHP do you use? Depending on it there could be different solutions. Commented Mar 11, 2016 at 8:30
  • In future, please do not edit your question such that it invalidates existing answers. It is not fair on the users spent time writing the answers you invalidated. Next time, just ask a new question. Commented Mar 15, 2016 at 13:12

2 Answers 2

1

Example with 5.6 (argument unpacking is needed)

<?php

$r1 = [
    1 => [1, 2, "one"],
    2 => [5, 5, "two"],
];
$r2 = [
    1 => [null, 20, "one"],
    2 => [1, 0, "two"],
];

$sum = function (array $a1, array $a2) {
    return array_map(
        function($e1) {
            return (is_int($e1) || is_null($e1)) ? array_sum(func_get_args()) : $e1;
        },
        $a1,
        $a2
    );
};


$res = [];
foreach ($r1 as $k => $a) {
    $res[$k] = $sum($r1[$k], $r2[$k]);
}

var_dump($res);

// Output:
array(2) {
  [1] =>
  array(3) {
    [0] =>
    int(1)
    [1] =>
    int(22)
    [2] =>
    string(3) "one"
  }
  [2] =>
  array(3) {
    [0] =>
    int(6)
    [1] =>
    int(5)
    [2] =>
    string(3) "two"
  }
}

https://3v4l.org/i10rJ

Sign up to request clarification or add additional context in comments.

7 Comments

Se updated question, also, I want the array key to be the same, to start with 1 then go on to 2 etc.
And again: what's your PHP version?
Running version 5.3 Error is: Parse error: syntax error, unexpected '[' in /home/conn/subdomener/projects/functions.php on line 1067
5.3 is a very old version, please update. Otherwise you should change all the [*] to array(*).
Returned Catchable fatal error: Argument 2 passed to {closure}() must be an array, null given, called in /home/connecti/subdomener/projects/functions.php on line 1071 and defined in /home/connecti/subdomener/projects/functions.php on line 1058
|
0

I hope this help.

<?php
//your needs
/* 
$d = array(
    0 => array(1400, 40, 1500, 30),
    1 => array(1500, 40, null, 30)
);
*/

//a
$a = array(
    0 => array(1400,20,null,15),
    1 => array(null, 20, null, 15)
);

//b
$b = array(
    0 => array(null,20,1500,15),
    1 => array(1500, 20, null, 15)
);

//c
$c = array_map(function () {
    $args = func_get_args();
    $sumArray = array();
    foreach ($args as $k=>$subArray) {
        foreach ($subArray as $id=>$value) {
            @$sumArray[$id]+=$value;
        }
    }
    return $sumArray;
}, $a, $b);

//result
print_r($c);
?>

Result

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.