这是我到目前为止的代码,尝试获取当前最高值,然后将该值与当前迭代的值进行比较。如果值匹配,则我将项目推送到新数组并从当前数组中取消设置。夹在将项目从一个阵列移动到另一个阵列之间,同时取消设置旧阵列。有什么帮助吗?
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++;
}
}}
我想要实现像这样的数组
$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'
]
];