Solved

# Calculating array values - some, not all

Posted on 2012-08-27
Medium Priority
257 Views
I need to calculate values in an array the precede a value in the middle of the array:

array:
``````\$score = array('2','2','2','3','3','3','3','3','6','9','5','9','5','10','5','5','6','7','9','12');
``````

Example:

If the \$_POST['score_value'] is \$score[7], for example, I need to SUM the six values that come before it in the array...

``````\$i = \$_POST['score_value']
if(\$score[\$i]) - sum all values that come before \$score[\$i]
``````

There are no constants - the \$_POST value might be any of the array values - I can identify which one, but don't know how to SUM the values that come before it -

Thanks in advance - you guys are great!
michael
0
Question by:michlcamp
• 2

LVL 18

Assisted Solution

Garry Glendown earned 1000 total points
ID: 38339528
Here's a quick hack ...
``````<?
\$score = array('2','2','2','3','3','3','3','3','6','9','5','9','5','10','5','5','6','7','9','12');

function mysum(\$pos,\$offset=0)
{
global \$score;
\$sum=0;

foreach (\$score as \$idx => \$val) {
if (\$idx+\$offset==\$pos) return(\$sum);
\$sum+=\$val;
}
return(\$sum);
}

print "With 1st array position as #0:\n";
print "The sum before position 7 is ".mysum(7) ."\n";
print "The sum before position 12 is ".mysum(12) ."\n";

print "With 1st array position as #1:\n";
print "The sum before position 7 is ".mysum(7,1) ."\n";
print "The sum before position 12 is ".mysum(12,1) ."\n";

?>
``````

Just drop the function into your code and you should be good to go ... depending on whether you want the first array position seen as "0" or "1", use the second argument (or alter the default)
0

LVL 25

Accepted Solution

ID: 38339545
``````\$score = array('2','2','2','3','3','3','3','3','6','9','5','9','5','10','5','5','6','7','9','12');

\$i = \$_POST['score_value'];
if (\$score[\$i] ) \$total = array_sum(array_slice(\$score, 0, \$i-1));

echo \$total;
``````
You may want to take a copy of \$score and slice it if you need it for other things.
0

Author Comment

ID: 38340254
Both of these solutions work perfectly - thanks! I'm gonna use the array_slice over the function, but both are valuable for the work I'm doing. Hope you don't mind I gave 50/50 points to each...

thanks again!
0

LVL 18

Expert Comment

ID: 38340539
Thanks ... I've not used the PHP function to add up the elements or the slice function before, with them being internal functions of PHP instead of programmed code, I assume they should perform better with very large arrays ... always good to learn something new ...
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creatâ€¦
###### Suggested Courses
Course of the Month13 days, 15 hours left to enroll