?
Solved

Calculating array values - some, not all

Posted on 2012-08-27
4
Medium Priority
?
257 Views
Last Modified: 2012-08-28
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');

Open in new window


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] 

Open in new window


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
Comment
Question by:michlcamp
  • 2
4 Comments
 
LVL 18

Assisted Solution

by:Garry Glendown
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";

?>

Open in new window


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

by:
lwadwell earned 1000 total points
ID: 38339545
what about ...
$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;

Open in new window

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

Author Comment

by:michlcamp
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

by:Garry Glendown
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question