ainbila Posted June 28, 2021 Share Posted June 28, 2021 is it possible to calculate sum of the element in the array_pop . i have to get sum of 3 maximum subject with grade from student . else if (($sub == 'BI') || ($sub == 'TSI') || ($sub == 'PAP') || ($sub == 'PAK')) { if ($res == 0) { $merit = 18; } if ($res == 1) { $merit = 16; } if ($res == 2) { $merit = 14; } if ($res == 3) { $merit = 12; } if ($res == 4) { $merit = 10; } if ($res == 5) { $merit = 8; } if ($res == 6) { $merit = 6; } if ($res == 7) { $merit = 4; } if ($res == 8) { $merit = 2; } if ($res == 9) { $merit = 0; } $main_add_merit1 = $merit; $main_add_merit1; $y[$i] = $main_add_merit1; //echo '<br>'; rsort($y); $first = array_pop($y); } Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/ Share on other sites More sharing options...
Barand Posted June 28, 2021 Share Posted June 28, 2021 Quicker to use array_slice() $y = [ 0, 4, 10, 8, 8, 10, 0, 14 ]; rsort($y); // descending sort /* check contents $y = Array ( [0] => 14 [1] => 10 [2] => 10 [3] => 8 [4] => 8 [5] => 4 [6] => 0 [7] => 0 ) } */ $top3total = array_sum(array_slice($y, 0, 3)); // get total of first 3 echo $top3total; // 34 Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/#findComment-1587624 Share on other sites More sharing options...
Barand Posted June 28, 2021 Share Posted June 28, 2021 BTW, have done an rsort(), array_pop() will get you the lowest value. Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/#findComment-1587626 Share on other sites More sharing options...
ainbila Posted June 28, 2021 Author Share Posted June 28, 2021 (edited) i've tried the array_slice .the first sum is right ,but after that it got wrong total and being looping decreasing .right now the array is giving right value . for example : 1210 161014 this is array for 2 student . so first student will get 12+10 ,and second will get 16+10+14 .this student get value based on merit grade of their 3 maximum subject from else if Edited June 28, 2021 by ainbila Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/#findComment-1587627 Share on other sites More sharing options...
Barand Posted June 28, 2021 Share Posted June 28, 2021 17 minutes ago, ainbila said: for example : 1210 161014 this is array for 2 student That doesn't resemble any array I've seen before. If you do echo '<pre>' . print_r( $array , true) . '</pre>'; (where $array is the name of the array you are trying to process - $sub, $s, $y or whatever it is next time you post) then post the output, I can see what you are processing. Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/#findComment-1587628 Share on other sites More sharing options...
ainbila Posted June 28, 2021 Author Share Posted June 28, 2021 (edited) rsort($y); echo '<pre>' . print_r( $y , true) . '</pre>'; output for 4 student: Array ( [0] => 16 [1] => 14 [2] => 12 [3] => 10 [4] => 8 [5] => 6 ) Array ( [0] => 16 [1] => 12 [2] => 10 [3] => 6 ) Array ( [0] => 16 [1] => 14 [2] => 12 [3] => 10 [4] => 6 ) Array ( [0] => 16 [1] => 14 [2] => 12 [3] => 10 [4] => 8 [5] => 6 ) this is the output of array after the sorting .and i want to sum 3 highest value .like 16+14+12 Edited June 28, 2021 by ainbila Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/#findComment-1587642 Share on other sites More sharing options...
ainbila Posted June 28, 2021 Author Share Posted June 28, 2021 i'm sorry if (($sub == 'SNT') || ($sub == 'LKJ') || ($sub == 'GRA') || ($sub == 'GKT') || ($sub == 'TEK') || ($sub == 'PJA') || ($sub == 'BIO') || ($sub == 'PJE') || ($sub == 'PJM') || ($sub == 'PSS') || ($sub == 'PEE') || ($sub == 'MUL') || ($sub == 'PDT') || ($sub == 'RKB') || ($sub == 'RT') || ($sub == 'SS')){ if ($res == 0) { $merit = 18; } if ($res == 1) { $merit = 16; } if ($res == 2) { $merit = 14; } if ($res == 3) { $merit = 12; } if ($res == 4) { $merit = 10; } if ($res == 5) { $merit = 8; } if ($res == 6) { $merit = 6; } if ($res == 7) { $merit = 4; } if ($res == 8) { $merit = 2; } if ($res == 9) { $merit = 0; } $main_add_merit = $merit; $k[$i] = $merit; rsort($k); $arrlength = count($k); for($x = 0; $x < 1; $x++) { $main_add_merit=$k[$x]; $main_add_merit; } } if ((($sub == 'BM') || ($sub == 'BI') || ($sub == 'SEJ') || ($sub == 'PQS') || ($sub == 'PSI')|| ($sub == 'BAT')|| ($sub == 'PI'))){ if ($res == 0) { $merit = 18; } if ($res == 1) { $merit = 16; } if ($res == 2) { $merit = 14; } if ($res == 3) { $merit = 12; } if ($res == 4) { $merit = 10; } if ($res == 5) { $merit = 8; } if ($res == 6) { $merit = 6; } if ($res == 7) { $merit = 4; } if ($res == 8) { $merit = 2; } if ($res == 9) { $merit = 0; } $main_add_merit1 = $merit; $s[$i] = $main_add_merit1; //echo '<br>'; rsort($s); echo '<pre>' . print_r( $s, true) . '</pre>'; $sum = array_sum(array_slice($s, 0, 3)); echo $sum; } } } there is error in my if else that got wrong subject. array_slice() also can be used for sum . i want to find 3 subject highest value ,the subject must be others than the first if else . Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/#findComment-1587645 Share on other sites More sharing options...
Barand Posted June 29, 2021 Share Posted June 29, 2021 8 hours ago, ainbila said: rsort($y); echo '<pre>' . print_r( $y , true) . '</pre>'; output for 4 student: Array ( [0] => 16 [1] => 14 [2] => 12 [3] => 10 [4] => 8 [5] => 6 ) Array ( [0] => 16 [1] => 12 [2] => 10 [3] => 6 ) Array ( [0] => 16 [1] => 14 [2] => 12 [3] => 10 [4] => 6 ) Array ( [0] => 16 [1] => 14 [2] => 12 [3] => 10 [4] => 8 [5] => 6 ) this is the output of array after the sorting .and i want to sum 3 highest value .like 16+14+12 Repeat the total calculation for each array. Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/#findComment-1587657 Share on other sites More sharing options...
ainbila Posted June 30, 2021 Author Share Posted June 30, 2021 21 hours ago, Barand said: Repeat the total calculation for each array. hi barand ,can you check my if else .there something wrong .the calculation is right actually Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/#findComment-1587682 Share on other sites More sharing options...
Barand Posted June 30, 2021 Share Posted June 30, 2021 3 hours ago, ainbila said: can you check my if else Not with your standard of formatting and indenting. It's impossible to spot where one ends and the next one starts and which are nested inside others. Quote Link to comment https://forums.phpfreaks.com/topic/312997-calculate-sum-from-array_pop/#findComment-1587687 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.