I want to combine the two arrays in my results
MY CODE
<?php
/* Designated level for each exp
Level 2 - 23 exp
Level 3 - 34 exp
Level 4 - 45 exp
Level 5 - 56 exp
Level 6 - 68 exp
Level 7 - 79 exp
Level 8 - 90 exp
Level 9 - 101 exp
Level 10 - 112 exp
Level 11 - 123 exp
Level 12 - 134 exp
Level 13 - 145 exp
Level 14 - 156 exp
Level 15 - 168 exp
Level 16 - 179 exp
*/
$limit = 100000-99318;
// Level
$arrlevel = array ('Level 2','Level 3','Level 4','Level 5','Level 6','Level 7','Level 8','Level 9','Level 10','Level 11','Level 12','Level 13','Level 14','Level 15','Level 16');
// Exp
$array = array (23,34,45,56,68,79,90,101,112,123,134,145,156,168,179);
$array = array_filter($array, function($var) use ($limit) {
return ($var <= $limit);
});
$num = count($array);
$total = pow(2, $num);
$out = array();
for ($i = 0; $i < $total; $i++) {
$comb = array();
for ($j = 0; $j < $num; $j++) {
// is bit $j set in $i?
if (pow(2, $j) & $i){
$comb[] = $array[$j];
}
}
if (array_sum($comb) == $limit)
{
$out[] = $comb;
}
}
array_multisort(array_map('count', $out), SORT_ASC, $out);
$out = array_unique($out, SORT_REGULAR);
$m = 1;
foreach($out as $result)
echo "<b>Possible Answer ". $m++. " : </b> " .implode(', ', $result)."
<br><br>";
?>
Output:
Possible Answer 1 :
23, 34, 45, 68, 79, 90, 112, 179
Possible Answer 2 :
23, 34, 45, 68, 79, 90, 123, 168
Possible Answer 3 :
23, 34, 45, 68, 79, 101, 112, 168
I want the ouput like this
Possible Answer 1 :
Level 2 - 23 | Level 3 - 34 | Level 4 - 45 | Level 6 - 68 | Level 7 - 79 | Level 8 - 90 | Level 10 - 112 | Level 16 - 179
-----------------------------------------
I want to combine the two arrays
(This program is about finding all combination to reach the result [Subset sum program])
You might use a mapper for the entries of Level n and the experience.
Then for the $result array in the loop you could use array_map and for each item that you are mapping use the value as the key for the $mapper array and implode using |
That will format all the possible answers in this format:
Possible Answer 1 :
Level 3 - 34 | Level 13 - 145 | Level 14 - 156 | Level 15 - 168 | Level 16 - 179
For example
$mapper = [
23 => "Level 2",
34 => "Level 3",
45 => "Level 4",
56 => "Level 5",
68 => "Level 6",
79 => "Level 7",
90 => "Level 8",
101 => "Level 9",
112 => "Level 10",
123 => "Level 11",
134 => "Level 12",
145 => "Level 13",
156 => "Level 14",
168 => "Level 15",
179 => "Level 16",
];
foreach($out as $result)
echo "<b>Possible Answer ". $m++. " : </b> " .implode(' | ', array_map(function($x) use ($mapper) {
return $mapper[$x] . " - " . $x;
}, $result))."
<br><br>";
Php demo
Just do it and do sort
$combine=array_combine($arrlevel,$array);
Related
I check the code and realized that it can't show repetitive numbers
MY CODE
<?php
/* Designated level for each exp
Level 2 - 23 exp
Level 3 - 34 exp
Level 4 - 45 exp
Level 5 - 56 exp
Level 6 - 68 exp
Level 7 - 79 exp
Level 8 - 90 exp
Level 9 - 101 exp
Level 10 - 112 exp
Level 11 - 123 exp
Level 12 - 134 exp
Level 13 - 145 exp
Level 14 - 156 exp
Level 15 - 168 exp
Level 16 - 179 exp
*/
$limit = 100000-99370;
// Level
$arrlevel = array ('Level 2','Level 3','Level 4','Level 5','Level 6','Level 7','Level 8','Level 9','Level 10','Level 11','Level 12','Level 13','Level 14','Level 15','Level 16');
// Exp
$array = array (23,34,45,56,68,79,90,101,112,123,134,145,156,168,179);
$array = array_filter($array, function($var) use ($limit) {
return ($var <= $limit);
});
$num = count($array);
$total = pow(2, $num);
$out = array();
for ($i = 0; $i < $total; $i++) {
$comb = array();
for ($j = 0; $j < $num; $j++) {
// is bit $j set in $i?
if (pow(2, $j) & $i){
$comb[] = $array[$j];
}
}
if (array_sum($comb) == $limit)
{
$out[] = $comb;
}
}
array_multisort(array_map('count', $out), SORT_ASC, $out);
$out = array_unique($out, SORT_REGULAR);
$m = 1;
$mapper = [
23 => "Level 2",
34 => "Level 3",
45 => "Level 4",
56 => "Level 5",
68 => "Level 6",
79 => "Level 7",
90 => "Level 8",
101 => "Level 9",
112 => "Level 10",
123 => "Level 11",
134 => "Level 12",
145 => "Level 13",
156 => "Level 14",
168 => "Level 15",
179 => "Level 16",
];
foreach($out as $result)
echo "<b>Possible Answer ". $m++. " : </b><br> " .implode(' , ', array_map(function($x) use ($mapper) {
return $mapper[$x] . " - " . $x;
}, $result))."
<br><br>";
My Input and Ouput
If i input 99318
the output is like this
Possible Answer 1 :
Level 10 - 112 , Level 11 - 123 , Level 12 - 134 , Level 13 - 145 , Level 15 - 168
I want to generate also the repetitive numbers too
But it cannot show some repetitive numbers answer like this
Possible Answer :
Level 4 - 45 , Level 10 - 112 , Level 11 - 123 , Level 11 - 123 , Level 12 - 134 , Level 13 - 145
You can see there's two Level 11 - 123
I want the ouput like this
Possible Answer :
Level 4 - 45 , Level 10 - 112 , Level 11 (x2) - 246 , Level 12 - 134 , Level 13 - 145
I want to group all repititive numbers and sum up them all
One option to get your result is to pass another value to array_map with the result of array_count_values.
Then inside the mapping you can determine to show the count for the number based on the index like $countValues[$x] just as for the mapper.
For example
foreach($out as $result) {
$countValues = array_count_values($result);
echo "<b>Possible Answer " . $m++ . " : </b><br> " . implode(' , ',
array_map(function ($x) use ($mapper, $countValues) {
$strCount = $countValues[$x] > 1 ? " (" . $countValues[$x] . ")" : "";
return $mapper[$x] . $strCount . " - " . $x;
}, array_unique($result))) . "
<br><br>";
}
That will give you a result like
Possible Answer 1 :
Level 2 - 23 , Level 6 - 68 , Level 7 (x2) - 79 , Level 9 - 101 , Level 10 - 112 , Level 15 - 168
Php demo with as a test a duplicate value 79 for $array
Each number has a corresponding value with it. There are many numbers which I can demonstate in a table here with their appropriate values:
[N] [V] N=Number V=Value
2 19
4 19
6 19
8 21
10 21
12 22
14 23
16 23
18 23
20 33
22 37
24 42
26 45
28 48
30 50
32 55
34 61
36 66
38 72
40 78
42 155
44 179
46 202
48 233
50 360
There is a process that a user will go through where they go from Number x to Number y. The values inbetween those numbers need to get added together. So for example, let's say a user goes from 16 to 38:
[N] [V] N=Number V=Value
2 19
4 19
6 19
8 21
10 21
12 22
14 23
[16][23]--
18 23 |
20 33 |
22 37 |
24 42 |
26 45 |
28 48 |---- All of these values get added together
30 50 |
32 55 |
34 61 |
36 66 |
[38][72]--
40 78
42 155
44 179
46 202
48 233
50 360
So the users total value would equal be:
23 + 23 + 33 + 37 + 42 + 45 + 48 + 50 + 55 + 61 + 66 + 72
Total Value = 555
The problem is, is that I have no idea how I to put this together in code. Like how to assign these values to their specific number and how to add those specific values together to get me a result. In PHP I simply do not know where to begin with this.
Also, the approximate values from the numbers can be represented by this equation:
v = 11.218e^(0.057n)
I would imagine this would be useful in making this whole process easier but I am still not sure how to go about implementing all of this. Any help would be very much apprieciated!
Put each each number with it's corresponding value into an array making number as key and value pair like this.
<?php
$arr = array(
2=> 19,
4=> 19,
6=> 19,
8=> 21,
10=> 21,
12=> 22,
14=> 23,
16=> 23,
18=> 23,
20=> 33,
22=> 37,
24=> 42,
26=> 45,
28=> 48,
30=> 50,
32=> 55,
34=> 61,
36=> 66,
38=> 72,
40=> 78,
42=> 155,
44=> 179,
46=> 202,
48=> 233,
50=> 360,
);
?>
Loop array with foreach loop like this
<?php
$sum = 0;
foreach($arr as $k => $v) {
if($k >= 16 && $k <= 38)
$sum += $v;
}
?>
There is another way using for loop statement, put both number in two separate array ($n and $v). Iterate the loop of the first array($n) and find the value from second array($v) through the index number of first array. But both array count should have same.
Example-
<?php
$n = array(2,4,6,8,10,12,14,16,18,20);
$v = array(19,19,19,21,21,22,23,23,23,33);
$sum = 0;
for($i=0, $i<count($n); $i++) {
if($n[$i] >= 16 && $n[$i] <= 38)
$sum += $v[$i];
}
?>
You would put your number and value pairs into an key / value array. So a shortened version of your test data would look like this:
$myDataStore = array(
"2" => "19",
"4" => "19",
"6" => "19",
"8" => "21",
"10" => "21",
"12" => "22",
"14" => "23",
"16" => "23",
"18" => "23",
"20" => "23"
);
Now you need a function to calculate your sum given a range as defined by starting and ending numbers.
function getRangeTotal($array, $startNumber, $endNumber){
$total = 0;
foreach($array as $key => $value){
if($key >= $startNumber && $key <= $endNumber){
$total = $total + $value;
}
}
return $total;
}
If you run the above function
getRangeTotal($myDataStore, 6, 12);
You'll get 83
Here is how you can do this using foreach
// first store you data to an array.
$kv = array(
2 => 19, 4 => 19, 6 => 19, 8 => 21,
10 => 21, 12 => 22, 14 => 23, 16 => 23,
18 => 23, 20 => 33, 22 => 37, 24 => 42,
26 => 45, 28 => 48, 30 => 50, 32 => 55,
34 => 61, 36 => 66, 38 => 72, 40 => 78,
42 => 155, 44 => 179, 46 => 202,48 => 233,
50 => 360
);
$start = 16;
$end = 32;
$sum = 0; //variable to keep sum
$n = 0; //variable to keep count
//loop through the array
foreach ($kv as $k => $v){
if ($k >=$start && $k <= $end){ //if key is in your range
$sum += $v; //add value to sum
$n ++; // increment count
}
}
$v = 11.218*pow(M_E,0.057*$n); //calculate the approximate values
echo "$sum\n$v\n";
Also refer to : pow and Predefined Constants
Is it possible to convert specific text file content into a php array ?
For example:
Text file
//group
1
// first id other values
1 5 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 1
end
//group
2
// first id other values
1 3 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 1
end
Return php array
1 => array(
1 => array(5, 7, 3, 83, 83, 83, 1),
2 => array...
),
2 => array(
1 => array(3, ...),
....
and so on, until end then next number group, and also ignore comments lines // or #
I've got another solution:
<?php
header('Content-type: text/plain');
$string = "//group
1
// first id other values
1 5 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 1
end
//group
2
// first id other values
1 3 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 2
end";
$string = preg_replace('/[^0-9 \n]/','',$string);
$array = array_filter(explode("\n", $string));
$temp_array = array();
$new_array = array();
$index = -1;
foreach($array as $key => $arr){
if(strlen(trim($arr)) == 1 && intval($arr) > 0){
$index = intval($arr);
}
else if(strlen(trim($arr)) > 4){
$temp_array = array_values(array_filter(explode(" ", $arr)));
$temp_index = $temp_array[0];
unset($temp_array[0]);
$new_array[$index][$temp_index] = $temp_array;
}
}
print_r(array_filter($new_array));
?>
Solved...
Text file
// group 1
1
1 4 3 "ssssssss"
end
// group 2
2
1 5 4 "ssssssss s"
end
// group 3
3
1 6 5 "ssssssss ss"
end
php convert code
$list = array();
$handle = fopen('Item.txt', 'r');
while(($line = fgets($handle)) !== false)
{
// skip comments
if(preg_match('!^//!', $line) || preg_match('/#/', $line))
{
continue;
}
// replace all spaces
$line = preg_replace('~"[^"]*"(*SKIP)(*F)|\s+~', ',', $line);
// skip blanks
if($line[0] == ',')
{
continue;
}
// define group
if(substr_count($line, ',') == 1 || substr_count($line, 'end') == 1)
{
if(substr_count($line, 'end') == 0)
{
$key = str_replace(',', '', $line);
}
continue;
}
// remove last comma
if(substr($line, -1) == ',')
{
$line = substr($line, 0, -1);
}
$arguments = explode(',', $line);
$id = $arguments[0];
unset($arguments[0]);
$list[$key][$id] = $arguments;
}
fclose($handle);
print_r($list);
output http://prntscr.com/6emxpq
How I can change this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 11
[8] => 21
[9] => 22
[10] => 23
[11] => 24
)
To this:
1-7, 11, 21-24
I have a list of numbers like this in PHP array, and I just want to make this list a little bit smaller.
2000: 3 6 7 11 15 17 25 36 42 43 45
2001: 2 3 4 5 6 9 10 11 12 13 34 37 45 46 47 48 49 50 51 52
2002: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 33 34 35 36 37 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2003: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
2004: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 21 22 23 24 25 26 27 28 29 30 31 32 33 34 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2005: 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2006: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2007: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Interesting task.
Here's a demo script that does exactly what you want.
Tweak to taste.
Code
<?php
$groups = array();
$active_group = 0;
$output = array();
$output_counter = 0;
$nums = array( 1, 2, 3, 4, 5, 6, 7, 11, 21, 22, 23, 24 );
foreach( $nums as $k => $num ) {
// if this isn't the first item, and the current number
// isn't one more than the previous one, increment the counter
if( $k !== 0 && $nums[$k] !== $nums[$k-1]+1 )
$active_group ++;
// add this number to a group
$groups[ $active_group ][] = $num;
}
// take the 1st and last of each group
foreach( $groups as $group ) {
$first = array_shift( array_values($group) );
$output[$output_counter][] = $first;
$last = array_pop( array_values($group) );
if( $first !== $last )
$output[$output_counter][] = $last;
$output_counter++;
}
echo '<pre>';
print_r($output);
?>
Output
Array
(
[0] => Array
(
[0] => 1
[1] => 7
)
[1] => Array
(
[0] => 11
)
[2] => Array
(
[0] => 21
[1] => 24
)
)
A single loop will do. You need need to keep track of the "previous" iteration's value and the "starting" value for storing ranged data.
Code:
$prev = -1; // initialize out of range
foreach ($numbers as $n) {
if (!isset($start)) { // first iteration
$start = $n; // declare $start
} elseif ($n != $prev + 1) { // not consecutive
$result[] = $start == $prev ? $prev : "$start-$prev"; // store single or ranged values
$start = $n; // update $start
}
$prev = $n; // declare / update $prev
}
$result[] = $start == $prev ? $prev : $start . '-' . $prev; // store final iteration data
echo implode(', ', $result); // comma delimit the values
Output from: $numbers = [1, 2, 3, 4, 5, 6, 7, 11, 21, 22, 23, 24]; (Demo)
1-7, 11, 21-24
Output from: $numbers = [1, 3, 5, 6, 11, 21, 22, 23, 24, 26]; (Demo)
1, 3, 5-6, 11, 21-24, 26
Here is a way to both compress an array of integers into the string format you want and to expand that string format back out to an array of integers.
function compress($expanded) {
$low = -1;
$prevNum = -1;
$expanded = array_unique($expanded);
sort($expanded, SORT_NUMERIC);
foreach($expanded as $num) {
if($low == -1) {
$low = $num;
} else if($num - $prevNum > 1) {
$compact[] = ($prevNum - $low >= 1) ? sprintf("%d-%d", $low, $prevNum) : $prevNum;
$low = $num;
}
$prevNum = $num;
}
if($low != -1 ) {
$compact[] = ($num - $low >= 1) ? sprintf("%d-%d", $low, $num) : $num;
}
return implode(",", $compact);
}
public static function expand($compact) {
$expanded = Array();
$compact = explode(",", $compact);
foreach($compact as $num) {
if( is_numeric($num) ) {
$expanded[] = $num;
} else {
list($low, $high) = explode("-", $num);
if( is_numeric($low) && is_numeric($high) && $low < $high) {
for($i = $low;$i <= $high;$i++) {
$expanded[] = $i;
}
}
}
}
return $expanded;
}
//Placeholder array
$b = array();
// Slice array (where to slice)
$s = array(11, 21);
foreach ($array as $year => $a) {
for($i = 0; $i < count($a); $i++) {
for($ii = 0; $ii < count($s); $ii++) {
if($i == 0) {
$b[$year]['<' . $s[$ii]][] = $a[$i];
break;
} else if ( isset($a[$i+1]) && $a[$i] < $s[$ii] && $a[$i+1] >=$s[$ii]){
$b[$year]['<' . $s[$ii]][] = $a[$i];
if (isset($s[$ii+1])) {
$b[$year]['<' . $s[$ii+1]][] = $a[$i+1];
} else {
$b[$year]['>' . $s[$ii]][] = $a[$i+1];
}
break;
} else if ( !isset($s[$ii+1]) && $i == count($a) - 1) {
$b[$year]['>' . $s[$ii]][] = $a[$i];
break;
}
}
}
}
$array
The list of numbers
OUTPUT ($b):
array
2000 =>
array
'<11' =>
array
0 => int 3
1 => int 7
'<21' =>
array
0 => int 11
1 => int 17
'>21' =>
array
0 => int 25
1 => int 45
2001 =>
array
'<11' =>
array
0 => int 2
1 => int 10
'<21' =>
array
0 => int 11
1 => int 13
'>21' =>
array
0 => int 34
1 => int 52
2002 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 52
2003 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 51
2004 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 19
'>21' =>
array
0 => int 21
1 => int 52
2005 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 52
2006 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 52
2007 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 52
NOTE: Just change the values (11) and (21) to suit your needs. You can add more values.
Heres an example:
$query = "SELECT '1-11' Range, COUNT(rank) rank
FROM promoted WHERE rank between 1 and 11
union all
SELECT '12-21' Range, COUNT(rank) rank
from promoted
where rank between 12 and 21
union all
SELECT '22-31' Range, count(rank) rank
from promoted
where rank between 22 and 31
union all
SELECT '32-40' Range, count(rank) rank
from promoted
where rank between 22 and 31
union all
SELECT rank, count(rank) FROM promoted WHERE rank = '40'";
I am working with database data that manipulates college students exam results. Basically, I am pulling the records from a MySQL database and pulling one class at any given time. I want to rank the students with the highest performer given the rank of 1.
Here is an illustration;
Marks: 37, 92, 84, 83, 84, 65, 41, 38, 38, 84.
I want to capture MySQL data as a single array. Once I have the data in an array, I should then assign each student a position in the class such as 1/10 (number 1, the 92 score), 4/10 etc. Now the problem is that if there is a tie, then the next score skips a position and if there are 3 scores at one position then the next score skips 2 positions. So the scores above would be ranked as follows;
92 - 1
84 - 2,
84 - 2,
84 - 2,
83 - 5,
65 - 6,
41 - 7,
38 - 8,
38 - 8 ,
37 - 10
The grading system requires that the number of positions (ranks, if you will) will be maintained, so we ended up with 10 positions in this class since positions 3, 4, 5 and 9 did not have any occupants. (The alternative of filling every number will have given us only 8 positions!)
Is it possible (humanly/programmatically possible) to use PHP to rank the scores above in such a way that it can handle possible ties such as 4 scores at one position? Sadly, I could not come up with a function to do this. I need a PHP function (or something in PHP) that will take an array and produce a ranking as above.
If it's possible to do this with MySQL query data without having it in an array, then that will also be helpful!
I assume the grades are already sorted by the database, otherwise use sort($grades);.
Code:
$grades = array(92, 84, 84, 84, 83, 65, 41, 38, 38, 37);
$occurrences = array_count_values($grades);
$grades = array_unique($grades);
foreach($grades as $grade) {
echo str_repeat($grade .' - '.($i+1).'<br>',$occurrences[$grade]);
$i += $occurrences[$grade];
}
Result:
92 - 1
84 - 2
84 - 2
84 - 2
83 - 5
65 - 6
41 - 7
38 - 8
38 - 8
37 - 10
EDIT (Response to discussion below)
Apparently, in case the tie occurs at the lowest score,
the rank of all lowest scores should be equal to the total count of scores.
Code:
$grades = array(92, 84, 84, 84, 83, 65, 41, 38, 37, 37);
$occurrences = array_count_values($grades);
$grades = array_unique($grades);
foreach($grades as $grade) {
if($grade == end($grades))$i += $occurrences[$grade]-1;
echo str_repeat($grade .' - '.($i+1).'<br>',$occurrences[$grade]);
$i += $occurrences[$grade];
}
Result:
92 - 1
84 - 2
84 - 2
84 - 2
83 - 5
65 - 6
41 - 7
38 - 8
37 - 10
37 - 10
$scores = array(92, 84, 84, 84, 83, 65, 41, 38, 38, 37);
$ranks = array(1);
for ($i = 1; $i < count($scores); $i++)
{
if ($scores[$i] != $scores[$i-1])
$ranks[$i] = $i + 1;
else
$ranks[$i] = $ranks[$i-1];
}
print_r($ranks);
I needed to end up with a map of values to rank. This method may be more efficient for the original question too.
public static function getGrades($grades)
{
$occurrences = array_count_values($grades);
krsort($occurrences);
$position = 1;
foreach ($occurrences as $score => $count) {
$occurrences[$score] = $position;
$position += $count;
}
return $occurrences;
}
If you print_r on $occurrences you get
Array
(
[92] => 1
[84] => 2
[83] => 5
[65] => 6
[41] => 7
[38] => 8
[37] => 10
)
Based on the original answer, so thanks!
Using array_count_values() followed by a foreach() is doing 2 loops over the input array, but this task can be done will one loop (minimizing/optimizing the time complexity).
Code: (Demo)
// assumed already rsort()ed.
$scores = [92, 84, 84, 84, 83, 65, 41, 38, 38, 37];
$gappedRank = 0;
$result = [];
foreach ($scores as $score) {
++$gappedRank;
$gappedRanks[$score] ??= $gappedRank;
$result[] = [$score => $gappedRanks[$score]];
}
var_export($result);
For a flat, associative lookup array of scores and their rank, unconditionally increment the counter and only push a new element into the lookup array if the key will be new. (Demo)
$gappedRank = 0;
$lookup = [];
foreach ($scores as $score) {
++$gappedRank;
$lookup[$score] ??= $gappedRank;
}
var_export($lookup);
The first snippet provides "gapped ranking". I have another answer which implements a similar approach but with a different input data structure and with the intent of modifying row data while looping.
Get dense rank and gapped rank for all items in array
In the realm of ranking, there is also "dense ranking". See my time complexity optimized answers at:
Populate multidimensional array's rank column with dense rank number
Add order column to array to indicate rank from oldest to youngest