2

I have the result from database.

Array
(
    [0] = stdClass Object
    (
        [name] = First
        [sum] = 3,8,...
    )

    [1] = stdClass Object
    (
        [name] = Second
        [sum] = -1,0,...
    )

    [2] = stdClass Object
    (
        [name] = Third
        [sum] = 2,-1...
    )

)

So now I need to sum all in column "sum".

I need to get result like

$final = (4, 7,...);

I have transformed sum to array throw explode() and then tried with foreach

for example

foreach ($result as $k=>$subArray) {
   $arrayNumbers = explode(",",$subArray->sum);

    foreach ($arrayNumbers as $key => $value) {
     $sumArray[] = $value];
     $stepToSum2[] = array_sum($sumArray);

  }
  unset($arrayNumb);
}

Not sure that my example working because I'm already stuck with commented code.

Anyway, I with some manipulations I can get or sum right for the first numbers (5) or the sum of my array (11).

The same result with this

$sum = array_sum(array_map(function($var) {
  return $var['sum'];
}, $myResultArray));

I have searched for the answer but most of the answers only for two arrays, but in same tables, I have more than 5 arrays, so I can't figure out how to implement this.

4
  • Do you have always the same structure for the sum attribute in your database? Commented Oct 14, 2016 at 14:03
  • number 1, number 2 ? Commented Oct 14, 2016 at 14:03
  • or there can be x numbers per row Commented Oct 14, 2016 at 14:03
  • 1
    There are more than 100 numbers for each one separated by comma, but always with the same number. The structure is the same. Commented Oct 14, 2016 at 14:13

3 Answers 3

4

array_reduce is good for reducing an array to a single value as you're doing here. It takes an array and a function that updates a "carry" value for each item in your array.

$result = array_reduce($your_array, function($carry, $item) {
    foreach (explode(',', $item->sum) as $key => $value) {
        $carry[$key] = $value + (isset($carry[$key]) ? $carry[$key] : 0);
        // (OR $carry[$key] = $value + ($carry[$key] ?? 0); in PHP 7)
    }
    return $carry;
}, []);
Sign up to request clarification or add additional context in comments.

4 Comments

Is it only for two numbers in SUM?
Because my sum looks like [sum] => 3.96,8.24,11.64,14.79,16.16,17.38,17.85,18.28,18.28
Oh, I see. Yes, it's just for two. I based it on what was shown in your question, but I can update it to show how it could work for more.
Update, please. I'm sure it will be helpful in future.
1

Since you're already creating an array:

foreach ($result as $subArray) {
   $arrayNumbers[] = explode(",", $subArray->sum);
}

$first  = array_sum(array_column($arrayNumbers, 0));
$second = array_sum(array_column($arrayNumbers, 1));

Comments

0

Try this :

foreach ($result as $k => $subArray) {
    $arrayNumbers = explode(",",$subArray->sum);

    foreach ($arrayNumbers as $key => $value) {
        $sumArray[$key] = isset($sumArray[$key]) ? $sumArray[$key] : 0;
        $sumArray[$key] += $value;
    }
}
print_r($sumArray);

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.