Here is the code I have so far, trying to get the current highest value then comparing the value with the value of current iteration. If the values match, then I push the item to the new array and unset it from the current array. Stuck in between shifting the items from one array to another and at the same time unsetting the old array. Any help?
public function rank()
{
$students = array(
array(
'name' => 'Philemon',
'score' => '90'
),
array(
'name' => 'Michael',
'score' => '86'
),
array(
'name' => 'Lameck',
'score' => '87'
),
array(
'name' => 'Tallam',
'score' => '90'
),
array(
'name' => 'Joel',
'score' => '82'
),
array(
'name' => 'Dennis',
'score' => '85'
),
array(
'name' => 'Caleb',
'score' => '83'
),
array(
'name' => 'Eric',
'score' => '82'
),
array(
'name' => 'Harold',
'score' => '88'
),
array(
'name' => 'Allan',
'score' => '82'
),
array(
'name' => 'Joseph',
'score' => '87'
)
);
$students_unsorted = $students;
$students_sorted = [];
$rank = 1;
foreach ($students_unsorted as $student) {
$current_score = $this->_maxValueInArray($students, 'score');
if ($student['score'] == $current_score) {
$student['rank'] == $rank;
$rank++;
}
}}
I want to achieve an array like this
$students = [
[
'name' => 'Philemon',
'score' => '90',
'rank' => '1'
],
[
'name' => 'Michael',
'score' => '86',
'rank' => '4'
],
[
'name' => 'Lameck',
'score' => '87',
'rank' => '3'
],
[
'name' => 'Tallam',
'score' => '90',
'rank' => '1'
],
[
'name' => 'Joel',
'score' => '82',
'rank' => '7'
],
[
'name' => 'Dennis',
'score' => '85',
'rank' => '5'
],
[
'name' => 'Caleb',
'score' => '83',
'rank' => '6'
],
[
'name' => 'Eric',
'score' => '82',
'rank' => '7'
],
[
'name' => 'Harold',
'score' => '88',
'rank' => '2'
],
[
'name' => 'Allan',
'score' => '82',
'rank' => '7'
],
[
'name' => 'Joseph',
'score' => '87',
'rank' => '3'
]
];