Multideminsional数组排序和向数组PHP添加键

时间:2018-03-26 07:20:13

标签: php arrays loops

我正在构建学生成绩系统现在我想根据他们的平均成绩对学生进行排序,我有一个像这样的数组

Array (
    [0] => Array (
        [student] => STD-000001
        [marks] => 78
        [total_subjects] => 1
        [avarage] => 78
    )
    [1] => Array (
        [student] => STD-000002
        [marks] => 60.4
        [total_subjects] => 1
        [avarage] => 60.4
    )
    [2] => Array (
        [student] => STD-000013
        [marks] => 0
        [total_subjects] => 0
        [avarage] => 0
    )
    [3] => Array (
        [student] => STD-000014
        [marks] => 0
        [total_subjects] => 0
        [avarage] => 0
    )
)

现在我想按avarage键对数组进行排序,然后通过添加新键position创建一个新数组。我期待像这样的数组

Array (
    [0] => Array (
        [student] => STD-000001
        [marks] => 78
        [total_subjects] => 1
        [avarage] => 78
        [position] => 1 )
    [1] => Array (
        [student] => STD-000002
        [marks] => 60.4
        [total_subjects] => 1
        [avarage] => 60.4
        [position] => 2
    )
    [2] => Array (
        [student] => STD-000013
        [marks] => 0
        [total_subjects] => 0
        [avarage] => 0
        [position] => 3
    )
    [3] => Array (
        [student] => STD-000014
        [marks] => 0
        [total_subjects] => 0
        [avarage] => 0
        [position] => 3
    )
)

正如您可以看到average之前的position越大,如果average匹配,它应该具有相同的position值。 我尝试过像

这样的方法
$class_students is the array above
   $sort_col = [];
        foreach ($class_students as $key => $row) {
            $sort_col[$key] = $row['avarage'];
        }
        array_multisort($sort_col, SORT_DESC, SORT_NUMERIC, $class_students);

这可以通过对“平均值”进行排序来实现,但我无法找到在数组中添加新键“位置”的方法。

4 个答案:

答案 0 :(得分:1)

首先使用usort,如下所示:

usort($class_students, function ($a, $b) {
    return $a['avarage'] < $b['avarage'];
});

最后,使用以下内容添加位置:

$pos = 1;
foreach ($class_students as $key => $value) {
    if ($key > 0 && $value['avarage'] < $class_students[$key - 1]['avarage']) {
        $pos += 1;
    }
    $class_students[$key]['position'] = $pos;
}

我拖拽了原始数组,并测试了上面的代码,这是输出:

Array
(
    [0] => Array
        (
            [student] => STD-000001
            [marks] => 78
            [total_subjects] => 1
            [avarage] => 78
            [position] => 1
        )

    [1] => Array
        (
            [student] => STD-000002
            [marks] => 60.4
            [total_subjects] => 1
            [avarage] => 60.4
            [position] => 2
        )

    [2] => Array
        (
            [student] => STD-000014
            [marks] => 0
            [total_subjects] => 0
            [avarage] => 0
            [position] => 3
        )

    [3] => Array
        (
            [student] => STD-000013
            [marks] => 0
            [total_subjects] => 0
            [avarage] => 0
            [position] => 3
        )

)

答案 1 :(得分:0)

您可以使用array_column($class_students, 'avarage')排序,这将删除您的foreach循环。你可以通过比较avg。

来遍历数组并在其中添加新的键值
array_multisort(array_column($class_students, 'avarage'), SORT_DESC, SORT_NUMERIC, $class_students);
$last_avg = "";
$pos = 0;
foreach ($class_students as $key => &$value) {
  $avg = $value['avarage'];
  if($last_avg != $avg)
    $pos++;
  $value['position']=$pos;
  $last_avg = $avg;   

}

答案 2 :(得分:-1)

usort是您应该使用的功能。

function compare($a, $b)
{
    return $b->average - $a->average;
}

usort($students, "compare");

至于职位 - 你不需要职位资产。

在对数组进行排序后,只需使用索引。

foreach ($students as $key => $value) {
    $position = $key+1;
    //if you really insist on having that property:
    $students[$key]['position'] = $key+1;
}

答案 3 :(得分:-1)

您可以使用array_column获取所有值并对其进行排序 然后在循环中使用新的数组键来构建结果数组并添加位置。

$avarage = array_column($arr, "avarage");

Arsort($avarage); //sort on values.
$i=0;
Foreach($avarage as $key => $val){
    $res[$i] = $arr[$key];
    $res[$i]['position'] = $i+1;
    $i++;
}
Var_dump($res);

https://3v4l.org/8XUED