Jump to content

calculate sum from array_pop


ainbila

Recommended Posts

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);
  

                    }
 




 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 by ainbila
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 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 by ainbila
Link to comment
Share on other sites

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 . 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.