1

How can I get the highest value from an array? For example, I'm trying to get the highest result of each quiz instead of multiple results of the same test.

I have an array that looks like this:

[
   {
      "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"
   }
]

I then rearrange the array to the following structure to make it easier to work with. this is the output of the array after I rearranged it.

[
   {
      "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"
         }
      ]
   }
]

I do this by using the following function:

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 );
}

I want to get the highest value for each test per user. Each user has 2 takes on a quiz, and I want to get the higher score of the same test. For example, if a user has taken quiz Foo Test 1 twice and gets 50 the first time and 70 the second time, I would like to return only the 70 for Foo Test 1.

I hope my question is clear enough. Thanks in advance.

4

1 回答 1

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"]);
}
于 2018-12-30T13:17:44.653 回答