# calculate sum from array_pop

## 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;
}

//echo '<br>';
rsort(\$y);

\$first = array_pop(\$y);

}

```

##### 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
(
 => 14
 => 10
 => 10
 => 8
 => 8
 => 4
 => 0
 => 0
)
}
*/

\$top3total = array_sum(array_slice(\$y, 0, 3));   // get total of first 3

echo \$top3total;                                 // 34```

##### Share on other sites

BTW, have done an rsort(), array_pop() will get you the lowest value.

##### Share on other sites

Posted (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 by ainbila
##### 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.

##### Share on other sites

Posted (edited)
``` rsort(\$y);

echo '<pre>' . print_r( \$y , true) . '</pre>';

output for 4 student:

Array
(
 => 16
 => 14
 => 12
 => 10
 => 8
 => 6
)
Array
(
 => 16
 => 12
 => 10
 => 6
)
Array
(
 => 16
 => 14
 => 12
 => 10
 => 6
)
Array
(
 => 16
 => 14
 => 12
 => 10
 => 8
 => 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
##### 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;
}

\$k[\$i] = \$merit;
rsort(\$k);
\$arrlength = count(\$k);

for(\$x = 0; \$x < 1; \$x++) {

}
}

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

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

##### Share on other sites

8 hours ago, ainbila said:
```
rsort(\$y);

echo '<pre>' . print_r( \$y , true) . '</pre>';

output for 4 student:

Array
(
 => 16
 => 14
 => 12
 => 10
 => 8
 => 6
)
Array
(
 => 16
 => 12
 => 10
 => 6
)
Array
(
 => 16
 => 14
 => 12
 => 10
 => 6
)
Array
(
 => 16
 => 14
 => 12
 => 10
 => 8
 => 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.

##### Share on other sites

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

##### Share on other sites

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.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×

• #### Activity

• Chat
×
• Create New...