FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » summing an array of arrays
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
summing an array of arrays [message #173501] Sat, 16 April 2011 00:06 Go to next message
jr is currently offline  jr
Messages: 4
Registered: January 2011
Karma: 0
Junior Member
$aryTemp=("A"=>Array(1,5,3),
"B"=>Array(15,20,52))

sum_subarrays_by_key($aryTemp, $aryTemp[$key]


function sum_subarrays_by_key($aryTemp, $key)
sum=0;
foreach($aryTemp as $sub_array){

$sum += $sub_array[$key];}
}
return $sum:
}

What I want to know how to do is change the above script to make it
add a 3rd array of arrays?
c=> Array(C1=> Array(17,4,8), C2=>Array(1,4,10))

thanks, Jan
Re: summing an array of arrays [message #173515 is a reply to message #173501] Sun, 17 April 2011 06:55 Go to previous messageGo to next message
Unrest is currently offline  Unrest
Messages: 10
Registered: April 2011
Karma: 0
Junior Member
Am Fri, 15 Apr 2011 17:06:12 -0700 schrieb jr:

> $aryTemp=("A"=>Array(1,5,3),
> "B"=>Array(15,20,52))
>
> sum_subarrays_by_key($aryTemp, $aryTemp[$key]
>
>
> function sum_subarrays_by_key($aryTemp, $key) sum=0;
> foreach($aryTemp as $sub_array){
>
> $sum += $sub_array[$key];}
> }
> return $sum:
> }
>
> What I want to know how to do is change the above script to make it add
> a 3rd array of arrays?
> c=> Array(C1=> Array(17,4,8), C2=>Array(1,4,10))
>
> thanks, Jan

Good morning Jan.


First off: I didn't even look at your code.
I implemented the solution to your problem with recursion - from scratch.


The example call sums to 0, as I subtract the sum of all the arrays'
values (197) from it.

It's lacking safeguards, but as the only intention was to show you how to
do such a task.. ;)

*------------ snip -----------*
function recurseSumArray($arr){
$sum=0;

foreach ($arr as $value){
if( ! is_array($value)){
$sum += $value;
}else{
$sum += recurseSumArray($value);
}
}

return $sum;
}

$arr = array('A'=>array(7,4,2),
'B'=>array(2,42,-1, 105),
'C'=>array(0,2,1, 33),
-197);
$sum = recurseSumArray($arr);

echo $sum;
*------------------------------*

If you've got any questions, feel free to ask.


Yours,
Michael
Re: summing an array of arrays [message #173516 is a reply to message #173515] Sun, 17 April 2011 14:58 Go to previous message
jr is currently offline  jr
Messages: 4
Registered: January 2011
Karma: 0
Junior Member
On Apr 16, 11:55 pm, Unrest <unr...@nullvector.org> wrote:
> Am Fri, 15 Apr 2011 17:06:12 -0700 schrieb jr:
>
>
>
>> $aryTemp=("A"=>Array(1,5,3),
>>                  "B"=>Array(15,20,52))
>
>> sum_subarrays_by_key($aryTemp, $aryTemp[$key]
>
>> function sum_subarrays_by_key($aryTemp, $key) sum=0;
>>  foreach($aryTemp as $sub_array){
>
>>        $sum += $sub_array[$key];}
>>   }
>> return $sum:
>> }
>
>> What I want to know how to do is change the above script to make it add
>> a 3rd array of arrays?
>> c=> Array(C1=> Array(17,4,8), C2=>Array(1,4,10))
>
>> thanks, Jan
>
> Good morning Jan.
>
> First off: I didn't even look at your code.
> I implemented the solution to your problem with recursion - from scratch.
>
> The example call sums to 0, as I subtract the sum of all the arrays'
> values (197) from it.
>
> It's lacking safeguards, but as the only intention was to show you how to
> do such a task.. ;)
>
> *------------ snip -----------*
> function recurseSumArray($arr){
>         $sum=0;
>
>         foreach ($arr as $value){
>                 if( ! is_array($value)){
>                         $sum += $value;
>                 }else{
>                         $sum += recurseSumArray($value);
>                 }
>         }
>
>         return $sum;
>
> }
>
> $arr = array('A'=>array(7,4,2),
>              'B'=>array(2,42,-1, 105),
>              'C'=>array(0,2,1, 33),
>              -197);
> $sum = recurseSumArray($arr);
>
> echo $sum;
> *------------------------------*
>
> If you've got any questions, feel free to ask.
>
> Yours,
> Michael

thanks I was trying to do it some more this morning and I was getting
the idea going but it is hard to think recursively so it helps to see
the example. I have some more like this thanks,
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Figure out path to php.ini
Next Topic: will code/solve problems for money
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Jul 08 12:34:21 GMT 2024

Total time taken to generate the page: 0.02792 seconds