Compare all possible combinations of element pairs in two arrays in PHP

I am trying to make a basic classification of binary strings in PHP. (The programming language is irrelevant, it is more convenient with PHP).

So basically I have 2 coordinate arrays:

$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) ];

I need to iterate over these arrays and get 2 pairs of points each time (the pair consists of a point from class A and another from class B). It is important to get all possible combinations. Once a particular point is in a pair, it cannot be present in another pair.

For example, the first two pairs:

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

To better explain myself, this is what I need:

enter image description here

When the first pair contains [1,1], [4,1], all possible combinations for the other pair are highlighted in yellow.

enter image description here

So far this is what I have:

$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];

                    }
                }
            }

        }
    }
}

, , ?

+4
3

, / " ", ,

$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

, :  1.  2.  3. Courtesian

, :

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

.

.

:

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);
   }
}
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);
0

Source: https://habr.com/ru/post/1613198/


All Articles