首页 > 解决方案 > 数组排序并将值推送到同一个键中

问题描述

我有一个如下结构的数组

$usersAttemptsInfo = [
    "200" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "300" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4],// less than 5
    "400" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "500" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3],// less than 5 & less than 4
    "600" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "700" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],// less than 5 & & less than 4 & less than 3
    "800" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "900" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4],// less than 5
    "1001" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3],// less than 5 & & less than 4
    "1002" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "1003" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],// less than 5 & & less than 4 & & less than 3
];

当我们第一次循环时totalCorrectQuestion是 5,所以我们的计数小于 5。根据我的数组,总共有 6 个用于 <5。所以我必须计算6/12 * 10012是 的总数$usersAttemptsInfo。数组 6/12*100 = 50

$usersAttemptsInfototalCorrectQuestion = 5我们必须更新percentile键和值的地方是50. percentile值在$usersAttemptsInfothen 循环中不应该运行。

下一个循环将运行totalCorrectQuestion4,所以我们的计数小于 4。根据我的数组,总共 5 是 <4。所以我必须计算 5/12*100 = 41.6。 我们必须更新键和值的$usersAttemptsInfo地方是totalCorrectQuestion = 4percentile41

下一个循环将运行totalCorrectQuestion3,所以我们的计数小于 3。根据我的数组,总共 2 是 <3。所以我必须计算 2/12*100 = 17.6。

$usersAttemptsInfototalCorrectQuestion = 4我们必须更新percentile键和值的地方是41. 下一个循环将运行totalCorrectQuestion2,所以我们的计数小于 2。根据我的数组总数 0 是 <2 所以我必须计算0/12 * 100

我的代码

$finalResult = [];
if (!empty($usersAttemptsInfo)) {
    $keys = array_keys($usersAttemptsInfo);
    array_multisort(array_column($usersAttemptsInfo, 'totalCorrectQuestion'), SORT_ASC, SORT_NUMERIC, $usersAttemptsInfo, $keys);
    $usersAttemptsInfo = array_combine($keys, $usersAttemptsInfo);
    $totalStudentAttemptedWorksheet = count($usersAttemptsInfo);
    foreach ($usersAttemptsInfo as $key => $userAttemptData) {
        $indexpossition = array_search($key, array_keys($usersAttemptsInfo));
        $percentile = $indexpossition / $totalStudentAttemptedWorksheet * 100;
        $finalResult[$key] = $userAttemptData;
        $finalResult[$key]["percentile"] = $percentile;
    }
}
echo "<pre>";
print_r($finalResult);

预期产出

$expected = [
    "200" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5, "precentile" => 50],
    "300" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4, "percentile" => 41],
    "400" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5, "precentile" => 50],
    "500" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3, "percentile" => 17],
    "600" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5, "precentile" => 50],
    "700" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2, "percentile" => 0],
    "800" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5, "precentile" => 50],
    "900" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4, "percentile" => 41],
    "1001" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3, "percentile" => 17],
    "1002" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5, "precentile" => 50],
    "1003" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2, "percentile" => 0],
];

我没有得到我必须使用的逻辑。我无法得到预期的答案。

标签: phparraysmultidimensional-array

解决方案



$usersAttemptsInfo = [
    "200" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "300" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4],
    "400" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "500" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3],
    "600" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "700" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],
    "800" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "900" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4],
    "1000" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 6],
    "1001" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3],
    "1002" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
    "1003" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2]
];




$totalUsersAttemptsInfo = count($usersAttemptsInfo);

function percentile($totalCorrectQuestion){
    global $totalUsersAttemptsInfo;
    return round($totalCorrectQuestion / $totalUsersAttemptsInfo * 100);

}

$finalResult = [];
if (!empty($usersAttemptsInfo)) {
    foreach ($usersAttemptsInfo as $key => $userAttemptData) {
        $finalResult[$key] = $userAttemptData;
        $total = 0;
        foreach($usersAttemptsInfo as $userAttemptData1){
            if($userAttemptData1['totalCorrectQuestion'] < $userAttemptData['totalCorrectQuestion']){
                $total++;
            }
        }
        $finalResult[$key]["percentile"] = percentile($total);
    }
}
ksort($finalResult); // to sort by key
echo "<pre>";
print_r($finalResult);


<pre>Array
(
    [200] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 5
            [percentile] => 50
        )

    [300] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 4
            [percentile] => 33
        )

    [400] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 5
            [percentile] => 50
        )

    [500] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 3
            [percentile] => 17
        )

    [600] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 5
            [percentile] => 50
        )

    [700] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 2
            [percentile] => 0
        )

    [800] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 5
            [percentile] => 50
        )

    [900] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 4
            [percentile] => 33
        )

    [1000] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 6
            [percentile] => 92
        )

    [1001] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 3
            [percentile] => 17
        )

    [1002] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 5
            [percentile] => 50
        )

    [1003] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 2
            [percentile] => 0
        )

)

推荐阅读