Author Topic: help needed with array summing  (Read 652 times)

Offline Aliss

  • Level 9
  • *
  • Posts: 54
  • Reputation: +1/-1
    • View Profile
help needed with array summing
« on: March 17, 2010, 07:31:45 AM »
Hello i have problem i wanna sum together from two arrays by array key but i encountered one problem.
if in second array is by 1 key more then in first array in the end that key is not added but if in first array is that key and in second don't have it then all is summed.

I hope someone can help me with this nightmare :D


Code: [Select]
$array1 = json_decode('{"str":"10","dex":"7","vit":"8","dmg":"12","def":"8","mag":"3"}', true);
$array2 = json_decode('{"str":"5","dex":"1","vit":"10","dmg":"5","def":"2"}', true);

function sum($array1,$array2)
{
$tmp = array();
foreach($array1 as $k=>$v) $tmp[$k] = $v + $array2[$k];
return($tmp);
}

print_r(sum($array1, $array2));


Offline dsheroh

  • Level 21
  • *
  • Posts: 235
  • Reputation: +6/-0
  • Perl Vicar
    • View Profile
    • Psi Rangers
Re: help needed with array summing
« Reply #1 on: March 17, 2010, 08:13:35 AM »
You actually have a slightly different problem than you think you do...  Because you're using the arrays associatively (as key/value pairs), the size doesn't matter.  The problem comes about when the second array contains keys which are not present in the first array because you're only looping over the first array's keys, so those are the only keys being put into the result.

You can avoid this problem by initializing your $tmp array as a copy of $array1 (so it will have all of $array1's keys), then loop over $array2 and add its values (which will create any keys in $array2 that are missing in $array1), thus ensuring that you'll get a sum for every key that's present in either array, regardless of their order.
Code: [Select]
function sum($array1,$array2) {
  $tmp = $array1;
  foreach($array2 as $k=>$v) $tmp[$k] += $array2[$k];
  return($tmp);
}

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: help needed with array summing
« Reply #2 on: March 17, 2010, 08:47:58 AM »
Or, you could take a different approach. With this approach, you can perform this operation on an unlimited* number of arrays in one function call:

*Note: you are limited by your available memory.

Code: (php) [Select]
function my_array_sum (array $a, array $b)
{
    $tmp = call_user_func_array ('array_merge_recursive', func_get_args ());

    foreach ($tmp as $k => $v)
    {
        $tmp [$k] = (is_array ($v)) ? array_sum ($v) : int_val ($v);
    }

    return $tmp;
}
Idiocy - Never underestimate the power of stupid people in large groups.


Offline Aliss

  • Level 9
  • *
  • Posts: 54
  • Reputation: +1/-1
    • View Profile
Re: help needed with array summing
« Reply #3 on: March 17, 2010, 12:57:03 PM »
Thx for your help all works now but Jgrow your method some how not worked for me.

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: help needed with array summing
« Reply #4 on: March 17, 2010, 01:33:33 PM »
lol that's what I get for not actually running it. Learn something new every day! Apparently you can't use func_get_args as a function parameter. Probably confuses it as to which function it's supposed to return the arguments for.

Additionally, I added an underscore to intval () which is a common mistake of mine. lol

Here's the corrected code with your examples supplied:
Code: (php) [Select]
function my_array_sum (array $a, array $b)
{
$args = func_get_args ();
$tmp = call_user_func_array ('array_merge_recursive', $args);

foreach ($tmp as $k => $v)
{
$tmp [$k] = (is_array ($v)) ? array_sum ($v) : intval ($v);
}

return $tmp;
}

// Do the test.
$array1 = json_decode('{"str":"10","dex":"7","vit":"8","dmg":"12","def":"8","mag":"3"}', true);
$array2 = json_decode('{"str":"5","dex":"1","vit":"10","dmg":"5","def":"2"}', true);

echo '<pre>';
print_r ($array1);
print_r ($array2);
print_r(my_array_sum($array1, $array2));
echo '</pre>';
Idiocy - Never underestimate the power of stupid people in large groups.


Offline Sagefire135

  • Level 14
  • *
  • Posts: 107
  • Reputation: +2/-0
    • View Profile
Re: help needed with array summing
« Reply #5 on: March 17, 2010, 03:05:32 PM »
wouldnt it be less difficult if you just added "mag":"0" to the second array?

Offline dsheroh

  • Level 21
  • *
  • Posts: 235
  • Reputation: +6/-0
  • Perl Vicar
    • View Profile
    • Psi Rangers
Re: help needed with array summing
« Reply #6 on: March 18, 2010, 05:05:41 AM »
wouldnt it be less difficult if you just added "mag":"0" to the second array?

I would argue that my suggested revision is no more complex/difficult than the original code and, while JGadrow's version is more complex, it provides additional flexibility/power in exchange for that complexity.

Verifying up front that both arrays have the same set of keys prior to calling the function, on the other hand, would either require writing a different function to compare the keys or remembering to manually set up the right set of keys in every pair of arrays that might ever be summed with each other.

So, yes, as a one-time thing, just adding "mag":"0" to the second array might be the easier solution, but, if this is something that will be reused, then building the function to be able to automatically handle arrays with mismatched sets of keys will be easier and take less time in the long run.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal