比较PHP

时间:2015-10-26 13:08:52

标签: php

我正在尝试在PHP中进行基本的二进制行分类。 (编程语言无关紧要,用PHP更舒服)。

所以基本上我有2个坐标数组:

$classA = [ new Point(1,1), new Point(1,2), new Point(3,3), new Point(1,5) ];
$classB = [ new Point(4,1), new Point(5,2), new Point(4,1), new Point(6,6) ];

我需要迭代这些数组,每次得到2对点(一对由classA中的一个点组成,另一个来自classB)。获得所有可能的组合非常重要。一旦特定点在一对中,它就不能存在于另一对中。

例如,前两对将是:

$pair1 = [$a[0], $b[0]];
$pair2 = [$a[1], $b[1]];

为了更好地解释自己,这就是我需要的:

enter image description here

当第一对包含[1,1],[4,1]时,另一对的所有可能组合都以黄色突出显示。

enter image description here

到目前为止,这就是我所拥有的:

$classA = [ new Point(1,1), new Point(1,2), new Point(3,3)];
$classB = [ new Point(4,1), new Point(5,2), new Point(4,1)];

$combinations = [];
$pair1 = [];
$pair2 = [];

$n = count($classA);
$m = count($classB);

for ($i=0; $i<$n; $i++){

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

        $pair1 = [ $classA[$i], $classB[$j] ];

        for ($z=0; $z<$n; $z++){

            if($z != $i && $z != $j){
                for ($y=0; $y<$m; $y++){
                    if($y != $i && $y != $j){

                        $pair2 = [ $classA[$z], $classB[$y] ];
                        $combinations[] = [$pair1, $pair2];

                    }
                }
            }

        }
    }
}

除了效率低下,这给了我很多重复,有没有办法只获得独特的组合?

3 个答案:

答案 0 :(得分:1)

我建议首先创建所有可能的组合,然后对每一对,用&#34;所有可能的组合&#34;替换行/列。虽然它可能不是最快的,但它应该起作用并且给你一般的想法

$allCombinations = Array();
foreach($classA as $value) {
    $column = Array();
    foreach($classB as $bPart) {
        $column[] = Array($bPart,$value);
    }
    $allCombinations[] = $column;
}

$possibleCombinations = Array();

$sizeA = count($classA);
$sizeB = count($classB);

for($a = 0; $a < $sizeA; $a++) {
    $column = Array();
    for($b = 0; $b < $sizeB; $b++) {
        $temp = $allCombinations;

        for($i = 0;$i < $sizeA;$i++) {
            $temp[$a][$i] = null;
        }

        for($i = 0;$i < $sizeB;$i++) {
            $temp[$i][$b] = null;
        }       

        // for $classA[$a] , $classB[$b] possible combinations are in temp now
        $column[] = $temp;
    }
    $possibleCombinations[] = $column;
}

现在,在$ possibleCombinations中,您可以看到给定A / B索引的可能组合

在第一个循环中,它只是创建所有可能的组合, 在第二个循环中,它将首先得到A和B值,复制所有组合数组,然后将A列和B行值设置为null(因为它们不能使用,对吧?),最后这个可能的组合存储在$ temp中并添加到$ possibleCombinations,

示例:

$ab = Array($classA[$i],$classB[$j]);
$possibleCombinationsForAB = $possibleCombinations[$i,$j];

答案 1 :(得分:0)

如果我理解正确,您需要以下内容:  1.选择一对  2.从数组中删除该对的元素  3.其余要素的贞节产品

执行上述操作的简化代码:

$classA = [ 1, 2, 3, 4 ];
$classB = [ 'a', 'b', 'c', 'd'];

function array_cartesian() {
    $_ = func_get_args();
    if(count($_) == 0)
       return array(array());
    $a = array_shift($_);
    $c = call_user_func_array(__FUNCTION__, $_);
    $r = array();
    foreach($a as $v)
        foreach($c as $p)
            $r[] = array_merge(array($v), $p);
    return $r;
}

$pair = [$classA[0], $classB[0]];
unset($classA[0]);
unset($classB[0]);
$pairs = array_cartesian($classA, $classB);
print_r($pairs);

该功能来自this question

编辑。

输出:

Array ( [0] => Array ( [0] => 2 [1] => b ) 
        [1] => Array ( [0] => 2 [1] => c ) 
        [2] => Array ( [0] => 2 [1] => d ) 
        [3] => Array ( [0] => 3 [1] => b ) 
        [4] => Array ( [0] => 3 [1] => c ) 
        [5] => Array ( [0] => 3 [1] => d ) 
        [6] => Array ( [0] => 4 [1] => b ) 
        [7] => Array ( [0] => 4 [1] => c ) 
        [8] => Array ( [0] => 4 [1] => d ) )

再加上您选择的那个,您拥有表中所述的所有可能的对。

EDIT2。

foreach ($classA as $key1 => $value1) {
   foreach ($classA as $key2 => $value2) {
     $A = $classA; // Copying here otherwise your array will be empty
     $B = $classB;
     $pair = [$value1, $value2];
     unset($A[$key1]);
     unset($B[$key2]);
     $pairs = array_cartesian($A, $B);
   }
}

答案 2 :(得分:0)

在两个数组上构建笛卡尔积而不重复点,记住数组索引。但是,这不会处理两个数组中存在的相同点。

$combinations = [];
$combined = [];

foreach ($classA as $keyA => $valueA) {
    foreach ($classB as $keyB => $valueB) {
        if (!isset($combined[$keyA][$keyB])) {
            $combined[$keyA][$keyB] = $combined[$keyB][$keyA] = true;
            $combinations[$keyA][$keyB] = [$valueA, $valueB];
        }
    }
}

var_dump($combinations);