简体   繁体   中英

Find highest value in multidimensional array and keep highest

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.

You already done most of the job by arranging the test elements per user. What remains is to filter this list.

Consider you have this list of "test-elements": (as John Doe in your example)

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

You can narrow it by:

$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

Now $res will output:

Array
(
    [0] => Array
        (
            [quiz] => Foo Test 1
            [score] => 100
        )
    [1] => Array
        (
            [quiz] => Foo Test 2
            [score] => 75
        )
)

Hope that helps!

Edited

Define the following function:

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

Use it in student_scores function. After foreach ( $quizzes as $x ) scope add new loop as:

foreach($out as &$elem) {
    $elem["quizes"] = filterQuizesByScore($elem["quizes"]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM