Comparing Multiple Arrays

I am trying to figure out how to write a tie-break function. The following entries are related for one reason or another, therefore, to break the tie, we sort each result, and then look at each line. The tie is broken at the first point, there is a difference.

In the example below, on the first pass, $ result [c] is eliminated, but a and b are still connected. Then in the second pass, a is eliminated, because it is greater than b. Thus, the result: b, a, c

$result[a] = array(1, 3, 4, 5);
$result[b] = array(1, 2, 3, 7);
$result[c] = array(2, 3, 3, 5);

And to make it even more difficult, I will not always have the same number of results for comparison. It can be more than 2.

I really hope this makes sense.

+5
source share
3 answers

php

if ($result['a'] > $result['b']) {

}

php , . , http://php.net/manual/en/language.operators.comparison.php

, .

asort($result);
print_r($result);

n- ,

asort($result);
print_r($result);
$ranked = array_keys($result);
$secondPlace = $ranked[1]; // a
print_r($result[$secondPlace]);

$letterRanks = array_flip($ranked);
echo $letterRanks['a']; // 1, for second 
+3
$i = 0;
while (count($result) > 1) {
   $winners = array();
   $best = PHP_INT_MAX;
   foreach ($result as $x) {
      if ($x[$i] == $best) {
         $winners[] = $x;
      } else if ($x[$i] < $best) {
         $winners = array($x);
         $best = $x[$i];
      }
   }
   $i++;
   $result = $winners;
}

... , . , , (1,2,3) (1,2) "". , , .

0

Here is a working solution. Let me know if you need a better explanation of how this works. I left debugging statements so that you can distinguish what it does. This solution will work with any number of competitors if each of them has the same $num_elementsin each array.

$result = array();
$result['a'] = array(1, 3, 4, 5);
$result['b'] = array(1, 2, 3, 7);
$result['c'] = array(2, 3, 3, 5);

$num_elements = 4; // In each array
$num_competitors = count( $result);

$finish_order = array();
$keys = $winners = array_keys( $result);

// $i is the current index into each competitor array
// $j is the current index into the $keys array (telling us the current competitor)
// $k is the next index into the $keys array (telling us the next competitor, i.e. the competitor to compare the current competitor with)
for( $i = 0; $i < $num_elements; $i++) {

    // If we've eliminated all but one winner, we're done!
    if( count( $winners) == 1) { 
        $finish_order[] = array_pop( $winners);
        break;
    }

    echo 'Element number ' . $i . "\n";

    for( $j = 0; $j < $num_competitors; $j++) {

        // If we've already eliminated this competitor, continue;
        if( !isset( $winners[$j])) continue;

        for( $k = $j + 1; $k < $num_competitors; $k++) {

            // If we've already eliminated this competitor, continue;
            if( !isset( $winners[$k])) continue;

            echo "\t - Who wins: " . $result[ $keys[$j] ][$i] . ' from ' . $keys[$j] . ' or ' . $result[ $keys[$k] ][$i] . ' from ' . $keys[$k] . "?\n";

            if( $result[ $keys[$j] ][$i] < $result[ $keys[$k] ][$i]) {

                echo "\t\t *** " . $keys[$k] . ' is out!' . "\n";
                $finish_order[] = $keys[$k];                
                unset( $winners[$k]);
            }

            if( $result[ $keys[$j] ][$i] > $result[ $keys[$k] ][$i]) {

                echo "\t\t *** " . $keys[$j] . ' is out!' . "\n";
                $finish_order[] = $keys[$j];                
                unset( $winners[$j]);
            }

        }
    }
}

echo "Game over - Result order is: " . implode( ', ', array_reverse( $finish_order));

Conclusion:

Element number 0
     - Who wins: 1 from a or 1 from b?
     - Who wins: 1 from a or 2 from c?
         *** c is out!
Element number 1
     - Who wins: 3 from a or 2 from b?
         *** a is out!
Game over - Result order is: b, a, c

Demo

0
source

All Articles