如何从数组中获得最大值?例如,我试图获得每个测验的最高结果,而不是同一测试的多个结果。
我有一个看起来像这样的数组:
[
{
"ID":"806",
"user_login":"123456789",
"post_title":"Foo Test 1",
"display_name":"John Doe",
"activity_meta_value":"100",
"activity_completed":"1543142130"
},
{
"ID":"806",
"user_login":"123456789",
"post_title":"Foo Test 2",
"display_name":"John Doe",
"activity_meta_value":"75",
"activity_completed":"1543144312"
},
{
"ID":"806",
"user_login":"123456789",
"post_title":"Foo Test 2",
"display_name":"John Doe",
"activity_meta_value":"75",
"activity_completed":"1543144528"
},
{
"ID":"1167",
"user_login":"987654321",
"post_title":"Foo Test 2",
"display_name":"Karen Eliot",
"activity_meta_value":"75",
"activity_completed":"1543156089"
},
{
"ID":"1167",
"user_login":"987654321",
"post_title":"Foo Test 2",
"display_name":"Karen Eliot",
"activity_meta_value":"100",
"activity_completed":"1543156480"
}
]
然后我将数组重新排列为以下结构,以使其更易于使用。这是我重新排列数组后的输出。
[
{
"id":"806",
"user_login":"123456789",
"user_name":"John Doe",
"quizes":[
{
"quiz":"Foo Test 1",
"score":"90",
"quiz_completed":"1543141990"
},
{
"quiz":"Foo Test 1",
"score":"100",
"quiz_completed":"1543142130"
},
{
"quiz":"Foo Test 2",
"score":"75",
"quiz_completed":"1543144312"
},
{
"quiz":"Foo Test 2",
"score":"75",
"quiz_completed":"1543144528"
}
]
},
{
"id":"1167",
"user_login":"987654321",
"user_name":"Karen Eliot",
"quizes":[
{
"quiz":"Foo Test 2",
"score":"75",
"quiz_completed":"1543156089"
},
{
"quiz":"Foo Test 2",
"score":"100",
"quiz_completed":"1543156480"
}
]
}
]
我通过使用以下功能来做到这一点:
function student_scores( $data ) {
global $wpdb;
$quizzes = $wpdb->get_results( " SELECT $wpdb->users.ID, $wpdb->users.user_login, $wpdb->posts.post_title, $wpdb->users.display_name, " . $wpdb->prefix . "learndash_user_activity_meta.activity_meta_value, " . $wpdb->prefix . "learndash_user_activity.activity_completed
FROM " . $wpdb->prefix . "learndash_user_activity
INNER JOIN $wpdb->users ON " . $wpdb->prefix . "learndash_user_activity.user_id = $wpdb->users.ID
INNER JOIN " . $wpdb->prefix . "learndash_user_activity_meta ON " . $wpdb->prefix . "learndash_user_activity.activity_id = " . $wpdb->prefix . "learndash_user_activity_meta.activity_id
INNER JOIN $wpdb->posts ON " . $wpdb->prefix . "learndash_user_activity.post_id = $wpdb->posts.ID
INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id
WHERE " . $wpdb->prefix . "learndash_user_activity.activity_type = 'quiz' AND " . $wpdb->prefix . "learndash_user_activity_meta.activity_meta_key = 'percentage' AND " . $wpdb->prefix . "usermeta.meta_value = " . $data['id'] . "GROUP BY " . $wpdb->prefix . "learndash_user_activity.activity_id ", ARRAY_A );
$out = array();
foreach ( $quizzes as $x ) {
$out[ $x['ID'] ]['id'] = $x['ID'];
$out[ $x['ID'] ]['user_login'] = $x['user_login'];
$out[ $x['ID'] ]['user_name'] = $x['display_name'];
$out[ $x['ID'] ]['quizes'][] = array(
'quiz' => $x['post_title'],
'score' => $x['activity_meta_value'],
'quiz_completed' => $x['activity_completed']
);
}
if ( empty( $out ) ) {
return new WP_Error(
'no_students',
'invalid group',
array(
'status' => 404
)
);
}
return array_values( $out );
}
我希望每个用户的每次测试都获得最高价值。每个用户有2个测验,我想获得同一测试的更高分数。例如,如果用户两次参加测验Foo Test 1,第一次获得50,第二次获得70,我只想返回70的Foo Test 1。
我希望我的问题足够清楚。预先感谢。
答案 0 :(得分:1)
您已经通过为每个用户安排测试元素来完成大部分工作。剩下的就是过滤该列表。
考虑您具有以下“测试元素”列表:(在您的示例中为John Doe
)
$a = array("quiz" => "Foo Test 1", "score" => "90");
$b = array("quiz" => "Foo Test 1", "score" => "100");
$c = array("quiz" => "Foo Test 2", "score" => "75");
$d = array("quiz" => "Foo Test 2", "score" => "75");
$quizes = array($a, $b, $c, $d);
您可以通过以下方式缩小范围:
$res = array();
foreach($quizes as $quize) {
if (!isset($res[$quize["quiz"]]) || ($res[$quize["quiz"]]["score"] < $quize["score"])) // is first test or this test higher then the previous
$res[$quize["quiz"]] = $quize;
}
$res = array_values($res); // because all you want are the quiz elements
现在$res
将输出:
Array
(
[0] => Array
(
[quiz] => Foo Test 1
[score] => 100
)
[1] => Array
(
[quiz] => Foo Test 2
[score] => 75
)
)
希望有帮助!
已编辑
定义以下功能:
function filterQuizesByScore($quizes) {
$res = array();
foreach($quizes as $quize) {
if (!isset($res[$quize["quiz"]]) || ($res[$quize["quiz"]]["score"] < $quize["score"]))
$res[$quize["quiz"]] = $quize;
}
return array_values($res);
}
在student_scores
函数中使用它。在foreach ( $quizzes as $x )
作用域之后,添加新的循环为:
foreach($out as &$elem) {
$elem["quizes"] = filterQuizesByScore($elem["quizes"]);
}