如何在 php 多维数组中获得最高分数
How to get max score in php multidimensional array
我有以下数组
$usersAttemptsInfo = [
"1" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],
"2" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4],
"3" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"4" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3],
"5" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"6" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],
"7" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3]
];
我想找出谁的分数最高,假设两个人的分数相同,我需要这两个值。
expected out put
$expected = [
"3" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"5" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5]
];
这里5
是最高分,所以我期待这个答案。
my code
$higiestScore = max(array_column($usersAttemptsInfo, 'totalCorrectQuestion'));
但它只返回一个分数。
max()
函数只会return只有一个值,结果没有问题。相反,您可以尝试使用 totalCorrectQuestion
过滤数组,以便在完成 max 函数后获得具有相同值的另一个数组。
更新:
你可以试试这个
<?php
$usersAttemptsInfo = [
"1" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],
"2" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4],
"3" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"4" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3],
"5" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"6" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],
"7" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3]
];
$highestscore = max(array_column($usersAttemptsInfo, 'totalCorrectQuestion'));
$results = array_filter($usersAttemptsInfo,
function ($item) {
global $highestscore;
if($item["totalCorrectQuestion"] == $highestscore) return $item;
});
var_dump($results);
?>
我有以下数组
$usersAttemptsInfo = [
"1" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],
"2" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4],
"3" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"4" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3],
"5" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"6" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],
"7" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3]
];
我想找出谁的分数最高,假设两个人的分数相同,我需要这两个值。
expected out put
$expected = [
"3" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"5" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5]
];
这里5
是最高分,所以我期待这个答案。
my code
$higiestScore = max(array_column($usersAttemptsInfo, 'totalCorrectQuestion'));
但它只返回一个分数。
max()
函数只会return只有一个值,结果没有问题。相反,您可以尝试使用 totalCorrectQuestion
过滤数组,以便在完成 max 函数后获得具有相同值的另一个数组。
更新: 你可以试试这个
<?php
$usersAttemptsInfo = [
"1" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],
"2" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 4],
"3" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"4" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3],
"5" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 5],
"6" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 2],
"7" => ["totalQuesAttempted" => 10, "totalCorrectQuestion" => 3]
];
$highestscore = max(array_column($usersAttemptsInfo, 'totalCorrectQuestion'));
$results = array_filter($usersAttemptsInfo,
function ($item) {
global $highestscore;
if($item["totalCorrectQuestion"] == $highestscore) return $item;
});
var_dump($results);
?>