Jump to content

Recommended Posts

I've got a file that I haven't worked on in awhile and am needing to make a little change to it. Not too sure of how to do it though.

The portion of the script I am looking at should be this:

 


$indexH[$var][] = array('firstname' => "$r2[firstname]", 'lastname' => "$r2[lastname]", 'number' => "$r2[number]", 'id' => "$r2[id]", 'q1' => "$r2[q1]", 'q2' => "$r2[q2]", 'q3' => "$r2[q3]", 'q1rt' => "$r2[q1rt]", 'q2rt' => "$r2[q2rt]", 'q3rt' => "$r2[q3rt]", 'q1s' => "$r2[q1s]", 'q2s' => "$r2[q2s]", 'q3s' => "$r2[q3s]", 'regid' => "$r2[regid]", 'eventid' => "$r2[eventid]");

 


$ir = 0;
foreach ( $indexH[$var] as $val ){

$q1 = $val[q1];
$q2 = $val[q2];
$q3 = $val[q3];

	$var2 = substr($var,0,5);
	$var2 = substr_replace($var2, "0", 4, 1);

if ($q1 < "$var2") { $q1 = '99.999'; } else { $q1 = $val[q1]; }
if ($q2 < "$var2") { $q2 = '99.999'; } else { $q2 = $val[q2]; }
if ($q3 < "$var2") { $q3 = '99.999'; } else { $q3 = $val[q3]; }

if (($q1 == $q2) && ($q1 == $q3)) {
$indexH[$var][$ir][bq] = "$q1";
} elseif (($q1 <= $q2) && ($q1 <= $q3)) {
$indexH[$var][$ir][bq] = "$q1";
} elseif ($q2 <= $q3) {
$indexH[$var][$ir][bq] = "$q2";
} else {
$indexH[$var][$ir][bq] = "$q3";
}

$ir++;
}


foreach ($indexH[$var] as $key => $value) {
     uasort($indexH[$var], 'compare');
}

 

 

This is taking three dfferent values (q1, q2, q3) getting the best value of the three and then sorting the array based on the best value chosen. Now the addition. If two entries in the array have the same best value resulting in a tie, I need to compare their qXs from that best value, higher qXs gets put above the lower. X = 1,2,3 depending on which is best value. If any more information is needed let me know, sort of hard to explain.

 

Link to comment
https://forums.phpfreaks.com/topic/214000-ordering-arrays/
Share on other sites

Oops! Forgot, this might help:

 

function compare ($a, $b) {
     if (!array_key_exists('bq', $a) || !array_key_exists('bq', $b)) return -1;
     if ($a['bq'] == $b['bq']) return 0;
     return $a['bq'] > $b['bq'] ? 1 : -1; // switch to reverse the ordering
}

Link to comment
https://forums.phpfreaks.com/topic/214000-ordering-arrays/#findComment-1113699
Share on other sites

It looks like from the above that the "best" value is the smallest value. You can just use min() instead of all those if/else statements.

 

Anyway, you don't need all that code to sort the array, just a simple function along with usort().

 

I don't see any qXs in the input array or your code, so I will assume it is for the minimum of the q1s, q2s, q3s.

 

This should be all you need:

function customSort($a, $b)
{
    //Primary sort on minimum value of q1, q2 & q3, if different
    $minA = min($a['q1'], $a['q2'], $a['q3']);
    $minB = min($b['q1'], $b['q2'], $b['q3']);
    if($minA != $minB) { return ($minA > $minB) ? 1 : -1; }
    //Secondary sort on minimum value of q1s, q2s & q3s
    $minA = min($a['q1s'], $a['q2s'], $a['q3s']);
    $minB = min($b['q1s'], $b['q2s'], $b['q3s']);
    if($minA != $minB) { return ($minA > $minB) ? 1 : -1; }
    //Both minimums are equal
    return 0;
}

usort($indexH[$var], 'customSort');
?>

 

Note: I always mix up the 1 and -1 for how they should sort. So reverse those values where is does not sort as needed.

Link to comment
https://forums.phpfreaks.com/topic/214000-ordering-arrays/#findComment-1113713
Share on other sites

I'll give your code a try and take a look at it. I was referring to q1s, etc and used X as a variable for them, sorry about that. I wasn't even thinking to mention it but, q1 q2 q3 would be lowest, but it's weird how it works. There is a target, say 4.70. You're trying to get as close as possible to that without going under, which I have taken and sorted out by creating an indexH and indexL. I didn't mention the L because I can do the same thing with it as I do with the H. There are 3 qualifying passes, if you run a 4.71 and then your other passes go under 4.70, you're still in a top spot because of your "best" time being that above 4.70 but very close to it. Hope that makes sense.

 

I threw your code in to my 'compare' function, it didn't seem to change the results. Here is a link so you can see the output:

http://www.outlawracing.com/index.php?page=ladders&event=8

If you look under the class '5.30 index'

I'm pretty bad with doing custom functions like this, but the bq is set as the "best qualifying time" and is the one that needs to be used for comparison against the others

Link to comment
https://forums.phpfreaks.com/topic/214000-ordering-arrays/#findComment-1113718
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.