-3

I have the following array output.

    Array
(
    [0] => Array
        (
            [student_id] => 39
            [scored] => 50
            [out_of] => 100
        )

    [1] => Array
        (
            [student_id] => 40
            [scored] => 80
            [out_of] => 100
        )

)

I want to calculate the percentage of the student and want to show the student on top whose percentage is higher. how do i do it? Can i change the order in the array itself? please help

I want the array to be like this

Array
(
    [0] => Array
        (             
            [student_id] => 40
            [scored] => 80
            [out_of] => 100
        )

    [1] => Array
        (
            [student_id] => 39
            [scored] => 50
            [out_of] => 100
        )

)
  • usort and a small self-written comparison function, and done. For more details on how to use, see suggested duplicate. – CBroe Apr 27 '18 at 12:19

2 Answers2

1

Use usort

usort($array, function($a, $b) {
    // This code will be executed each time two elements will be compared
    // If it returns a positive value, $b is greater then $a
    // If it returns 0, both are equal
    // If negative, $a is greater then $b
    return ($a['scored'] / $a['out_of']) <=> ($b['scored'] / $b['out_of']);
});

More details on this function: http://php.net/manual/en/function.usort.php
A list of all php sort algs: http://php.net/manual/en/array.sorting.php

Be aware that usort will modify the array itself, so DONT use $array = usort($array, ...)

tionsys
  • 151
  • 7
  • what if the array consist of many more records. This will work for array with only two records right? – Heisenberg Apr 27 '18 at 12:31
  • I updated the code with few more comments. The function will be called each time two elements are compared; if you have e.g. "1", "2" and "3" the function will first be called with "1"+"2", then with "1"+"3", then with "1"+"2". So it will work with any array. – tionsys Apr 27 '18 at 18:49
  • Thank you for your reply. I think usort works differently on php7. I am using php7 and it does not change the order of the array after performing the operations. – Heisenberg Apr 28 '18 at 08:50
  • Well it works like this in my php7-project. Updated the source, try replacing "-" with "<=>" (spaceship operator - see https://stackoverflow.com/questions/30365346/what-is-the-spaceship-operator-in-php-7) – tionsys Apr 29 '18 at 09:13
0

if your out_of is 100 each time means your scored itself is percentage

anyways you can use the below code

 function sortByScore($x, $y) {
  return $y['per'] - $x['per'];
 }

 $new_arr = array();
 foreach ($arr as $key => $value) {
     $per = ($value['scored']  / $value['out_of']  ) * 100;
     $value['per'] = $per;
     $new_arr[] = $value;
 }

first calculate the percentage and then sort by percentage

as your scored will be more if your out_of is different each time , so sorting on scored is not feasible

 usort($new_arr, 'sortByScore');
 echo "<pre>"; print_r($new_arr); 
Ash-b
  • 605
  • 6
  • 10
  • i tried it. I got the percentages in array but the order of the array did not change. It still shows the record with lesser percentage on top and more percentages record below it. – Heisenberg Apr 27 '18 at 12:56
  • this works on php5. I am working on php7 and it dosent work there. But thanks. – Heisenberg Apr 28 '18 at 11:52