0

So I've got the following array: http://pastebin.com/raw.php?i=AazcQUbG

In a part of code outside of the function below, I loop through this particular ordered array (ordered by the second dimension's 'team_points') and determine if there is a tie in 'team_points'. If a tie in team_points is determined, I check the tie_breaker of each.

I want a first dimension order switch to take place if a tie exists and the tie_breaker of the second item in the first dimension is less than the first item's.

The following function is not forcing a switch. Can someone please help me figure out why I am unable to make the positions in the first dimension of this array switch and suggest a way to accomplish that?

function array_swap($key1, $key2, $array) {
  $newArray = array ();
  foreach ($array as $key => $value) {
    if ($key == $key1) {
      $newArray[$key2] = $array[$key2];
    } elseif ($key == $key2) {
      $newArray[$key1] = $array[$key1];
    } else {
      $newArray[$key] = $value;
    }
  }
  return $newArray;
}
user1645914
  • 371
  • 6
  • 23
  • You don't compare the values at any point. How could they switch if they are in one relative state or another? I'm going to try to type up an alternate method. – Seth Battin Oct 26 '12 at 03:08
  • I do that in code outside of the function. I'll put that in the original post. – user1645914 Oct 26 '12 at 03:11
  • would it be happen that more than two teams share the same `team_points` ? – air4x Oct 26 '12 at 03:31
  • i've only seen it happen in a handful of cross country meets over the past 15 years where a three way tie exists. but you are right, it does happen. – user1645914 Oct 26 '12 at 04:30

1 Answers1

0

I think this might work better:

Edit: This function is meant to be run on your entire set of data, using your sorting fields as $key1 and $key2

function array_swap($key1, $key2, $array) {
  $newArray = array ();

  // I chose a for loop because each iteration deals with two elements
  for ($i = 1; $i < count($array); $i++) { 
    if ($array[$i][$key1] == $array[$i - 1][$key1]){
      if ($array[$i][$key2] < $array[$i - 1][$key2]) { //money-condition
        $newArray[$i - 1] = $array[$i];
        $newArray[$i]     = $array[$i - 1];
        $i++
      } else {
        $newArray[$i - 1] = $array[$i - 1];
      }
    } else {
      $newArray[$i - 1] = $array[$i - 1];
    }
  }

  // there is special condition if that last two elements are flipped
  // and $i was incremented, in which this last step won't be required
  if ($i < count($array)){
    $newArray[$i] = $array[$i];
  }
  return $newArray;
}
Seth Battin
  • 2,811
  • 22
  • 36
  • I really appreciate the effort but this is not what I was looking for. On top of that this function is not working as is and returns empty arrays. – user1645914 Oct 26 '12 at 04:45
  • @user1645914 I think you should post how you are using the function. Like I commented above, I don't understand how your function could compare anything. If my function is not working, I'll like to see what imputs you are giving it that cause it to fail. – Seth Battin Oct 26 '12 at 05:14