Calculating array values - some, not all

Posted on 2012-08-27
Last Modified: 2012-08-28
I need to calculate values in an array the precede a value in the middle of the 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


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!
Question by:michlcamp
    LVL 17

    Assisted Solution

    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;
            foreach ($score as $idx => $val) {
                    if ($idx+$offset==$pos) 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)
    LVL 25

    Accepted Solution

    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.

    Author Comment

    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!
    LVL 17

    Expert Comment

    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 ...

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now