michlcamp
asked on
Calculating array values - some, not all
I need to calculate values in an array the precede a value in the middle of the array:
array:
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...
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ...
ASKER
thanks again!