-2

I have the following code that case-insensitively groups associative elements in a flat array and sums the related values, but I don't really understand how it works.

function add_array_vals($arr) {
  $sums = [];
  foreach ( $arr as $key => $val ) {
    $key = strtoupper($key);
    if ( !isset($sums[$key]) ) {
      $sums[$key] = 0;
    }
    $sums[$key] = ( $sums[$key] + $val );
  }
  return $sums;
}

$array = ['KEY' => 5, 'TEST' => 3, 'Test' => 10, 'Key'=> 2];
$sums = add_array_vals($array);
var_dump($sums);

//Outputs
// KEY => int(7)
// TEST => int(13)

I have problem in two portion of above code one is:

if ( !isset($sums[$key]) ) {
   $sums[$key] = 0;
}

another is:

$sums[$key] = ( $sums[$key] + $val );

In this portion, how does it identify the same key of array to sum them (because keys position is dynamic)?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Chunk
  • 37
  • 7
  • Ah, you don't have the permission to ask @Jamie how [their answer](https://stackoverflow.com/a/74249935/2943403) works. Are you asking what `isset()` does and what `+` does? Have you researched? – mickmackusa Oct 31 '22 at 06:11
  • You are asking multiple questions, but questions must only ask one question at a time. – mickmackusa Oct 31 '22 at 06:18
  • Other "grouping and summing" pages: [Group every three rows in a 2d array and sum one column](https://stackoverflow.com/q/68486407/2943403) and [PHP Array Group and sum](https://stackoverflow.com/q/35606452/2943403) and [Group 2d array rows by one column and sum another column](https://stackoverflow.com/q/5269188/2943403) and [Group array data on one column and sum data from another column](https://stackoverflow.com/q/3286749/2943403) and [PHP array group by month and sum](https://stackoverflow.com/q/58902997/2943403) – mickmackusa Nov 01 '22 at 09:32

3 Answers3

0

This code summing by uppercase keys group.

First step: checked the key exists. If not exists then fill with initial zero value. This case occurs once for per key (base of group). It is necessary so that the key's non-existence does not pose a problem in the next step.

if ( !isset($sums[$key]) ) { $sums[$key] = 0; }

Second step: adds the value to the existed key container:

$sums[$key] = ( $sums[$key] + $val );

Marton
  • 11
  • 2
0
if ( !isset($sums[$key]) ) { $sums[$key] = 0; }

$sums starts as an empty array, but we're trying to add numbers to it. If we try to add a number to $sums[$key] that isn't initialized yet, we'll get an error, so we initialize it by setting it at zero the first time we encounter it.

$sums[$key] = ( $sums[$key] + $val );

This is the second part of the previous line. $sums[$key] is at this point either zero, thanks to the previous line, or an integer, because we've encountered it before in the loop.

The $key in $sums[$key] is going to be either 'TEST' or 'KEY' in this code.

symlink
  • 11,984
  • 7
  • 29
  • 50
-1
if ( !isset($sums[$key]) ) { $sums[$key] = 0; }

This means that if key or test has no value, assign the value 0.

For example, $array = ['KEY', 'TEST' => 3, 'Test' => 10, 'Key'=> 2];

The value of KEY will be 0.

$sums[$key] = ( $sums[$key] + $val );

The $sums[$key] is the sum of same elements. $sums[$key] + $val means add the previous sum value to the new element value.

James Risner
  • 5,451
  • 11
  • 25
  • 47
Emily Cs
  • 11
  • 1