Find highest value in multidimensional array and keep highest
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
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.
php arrays multidimensional-array highest
add a comment |
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.
php arrays multidimensional-array highest
So do you want to get the highest value for each test? or the highest value for each user_name? or the highest value for all?
– dWinder
Dec 30 '18 at 13:03
@DavidWinder 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. Hope it makes more sense now.
– CyberGoat
Dec 30 '18 at 13:07
add a comment |
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.
php arrays multidimensional-array 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.
php arrays multidimensional-array highest
php arrays multidimensional-array highest
edited Dec 30 '18 at 14:20
dWinder
4,41231028
4,41231028
asked Dec 30 '18 at 11:35
CyberGoatCyberGoat
82
82
So do you want to get the highest value for each test? or the highest value for each user_name? or the highest value for all?
– dWinder
Dec 30 '18 at 13:03
@DavidWinder 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. Hope it makes more sense now.
– CyberGoat
Dec 30 '18 at 13:07
add a comment |
So do you want to get the highest value for each test? or the highest value for each user_name? or the highest value for all?
– dWinder
Dec 30 '18 at 13:03
@DavidWinder 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. Hope it makes more sense now.
– CyberGoat
Dec 30 '18 at 13:07
So do you want to get the highest value for each test? or the highest value for each user_name? or the highest value for all?
– dWinder
Dec 30 '18 at 13:03
So do you want to get the highest value for each test? or the highest value for each user_name? or the highest value for all?
– dWinder
Dec 30 '18 at 13:03
@DavidWinder 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. Hope it makes more sense now.
– CyberGoat
Dec 30 '18 at 13:07
@DavidWinder 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. Hope it makes more sense now.
– CyberGoat
Dec 30 '18 at 13:07
add a comment |
1 Answer
1
active
oldest
votes
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"]);
}
I'm trying to figure out your code, but I can't understand how to implement into mine. Would it be possible to explain, please?
– CyberGoat
Dec 30 '18 at 13:48
@CyberGoat update my answer with more specific instruction for how to use it in your code
– dWinder
Dec 30 '18 at 14:02
Working perfectly! Thank you so much for clearing it out :)
– CyberGoat
Dec 30 '18 at 14:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977267%2ffind-highest-value-in-multidimensional-array-and-keep-highest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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"]);
}
I'm trying to figure out your code, but I can't understand how to implement into mine. Would it be possible to explain, please?
– CyberGoat
Dec 30 '18 at 13:48
@CyberGoat update my answer with more specific instruction for how to use it in your code
– dWinder
Dec 30 '18 at 14:02
Working perfectly! Thank you so much for clearing it out :)
– CyberGoat
Dec 30 '18 at 14:14
add a comment |
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"]);
}
I'm trying to figure out your code, but I can't understand how to implement into mine. Would it be possible to explain, please?
– CyberGoat
Dec 30 '18 at 13:48
@CyberGoat update my answer with more specific instruction for how to use it in your code
– dWinder
Dec 30 '18 at 14:02
Working perfectly! Thank you so much for clearing it out :)
– CyberGoat
Dec 30 '18 at 14:14
add a comment |
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"]);
}
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"]);
}
edited Dec 30 '18 at 14:01
answered Dec 30 '18 at 13:17
dWinderdWinder
4,41231028
4,41231028
I'm trying to figure out your code, but I can't understand how to implement into mine. Would it be possible to explain, please?
– CyberGoat
Dec 30 '18 at 13:48
@CyberGoat update my answer with more specific instruction for how to use it in your code
– dWinder
Dec 30 '18 at 14:02
Working perfectly! Thank you so much for clearing it out :)
– CyberGoat
Dec 30 '18 at 14:14
add a comment |
I'm trying to figure out your code, but I can't understand how to implement into mine. Would it be possible to explain, please?
– CyberGoat
Dec 30 '18 at 13:48
@CyberGoat update my answer with more specific instruction for how to use it in your code
– dWinder
Dec 30 '18 at 14:02
Working perfectly! Thank you so much for clearing it out :)
– CyberGoat
Dec 30 '18 at 14:14
I'm trying to figure out your code, but I can't understand how to implement into mine. Would it be possible to explain, please?
– CyberGoat
Dec 30 '18 at 13:48
I'm trying to figure out your code, but I can't understand how to implement into mine. Would it be possible to explain, please?
– CyberGoat
Dec 30 '18 at 13:48
@CyberGoat update my answer with more specific instruction for how to use it in your code
– dWinder
Dec 30 '18 at 14:02
@CyberGoat update my answer with more specific instruction for how to use it in your code
– dWinder
Dec 30 '18 at 14:02
Working perfectly! Thank you so much for clearing it out :)
– CyberGoat
Dec 30 '18 at 14:14
Working perfectly! Thank you so much for clearing it out :)
– CyberGoat
Dec 30 '18 at 14:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977267%2ffind-highest-value-in-multidimensional-array-and-keep-highest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
FROR7mrv UeujQLeSyoV982Al,J5egTfEZ,euNPI9ZjMjO QFs1ygKGOa Tx
So do you want to get the highest value for each test? or the highest value for each user_name? or the highest value for all?
– dWinder
Dec 30 '18 at 13:03
@DavidWinder 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. Hope it makes more sense now.
– CyberGoat
Dec 30 '18 at 13:07