比较多个阵列

时间:2012-06-02 19:04:01

标签: php

我正试图找出如何编写决胜局功能。以下记录都因各种原因而捆绑,因此为了打破平局,我们对每个结果进行排序,然后遍历每一行。领带在第一点被打破,存在差异。

在下面的例子中,第一次传递$ result [c]被消除,但a和b仍然被绑定。然后在第二次通过时,a被消除,因为它大于b。所以结果是b,a,c

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

为了使它更加复杂,我不会总是有相同数量的结果来比较。它可能超过2个。

我真的希望这是有道理的。

3 个答案:

答案 0 :(得分:3)

在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 

答案 1 :(得分:0)

$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)应该“赢”。此外,它不会执行任何数组边界检查或处理在比较所有元素后绑定多个数组的情况。

答案 2 :(得分:0)

这是一个有效的解决方案。如果您需要更好地解释它是如何工作的,请告诉我。我已经离开了调试语句,所以你应该能够辨别出它在做什么。此解决方案适用于任意数量的竞争对手,只要它们在每个阵列中都具有相同的$num_elements

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

<强>输出:

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