php - 数组排序并将值推送到同一个键中
问题描述
我有一个如下结构的数组
$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 * 100。 12
是 的总数$usersAttemptsInfo
。数组 6/12*100 = 50
$usersAttemptsInfo
totalCorrectQuestion = 5
我们必须更新percentile
键和值的地方是50
. percentile
值在$usersAttemptsInfo
then 循环中不应该运行。
下一个循环将运行totalCorrectQuestion
4,所以我们的计数小于 4。根据我的数组,总共 5 是 <4。所以我必须计算 5/12*100 = 41.6。 我们必须更新键和值的$usersAttemptsInfo
地方是totalCorrectQuestion = 4
percentile
41
下一个循环将运行totalCorrectQuestion
3,所以我们的计数小于 3。根据我的数组,总共 2 是 <3。所以我必须计算 2/12*100 = 17.6。
$usersAttemptsInfo
totalCorrectQuestion = 4
我们必须更新percentile
键和值的地方是41
. 下一个循环将运行totalCorrectQuestion
2,所以我们的计数小于 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],
];
我没有得到我必须使用的逻辑。我无法得到预期的答案。
解决方案
$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
)
)
推荐阅读
- excel - 如果两个单元格匹配返回正确但如果两个单元格都为零则忽略?
- c++ - 包括没有函数的常量
- python - sum() 的字节总和
- java - RestAssured XmlPath 数组
- qt - 这是 Qt5 文档中的错误吗?
- r - 分组数据和多个变量的滚动平均值
- java - java中使用递归的数组中元素的最后索引。但是我怀疑这段代码不应该返回第一个索引吗?
- apache-kafka - Kafka Connect JSON 模式似乎不支持“$ref”标签
- python - 类型错误:在 keras VQ-VAE 示例中,维度值必须是整数或无
- ios - IOS APP如何使用Outlook发送电子邮件