Domanda

Ciao a tutti, in fondo, ho un array:

array('a', 'b', 'c');

Ora ho eseguito attraverso una funzione di matrice permutazione e il risultato è:

Array
(
    [0] => Array
        (
            [0] => C
        )

    [1] => Array
        (
            [0] => B
        )

    [2] => Array
        (
            [0] => B
            [1] => C
        )

    [3] => Array
        (
            [0] => C
            [1] => B
        )

    [4] => Array
        (
            [0] => A
        )

    [5] => Array
        (
            [0] => A
            [1] => C
        )

    [6] => Array
        (
            [0] => C
            [1] => A
        )

    [7] => Array
        (
            [0] => A
            [1] => B
        )

    [8] => Array
        (
            [0] => B
            [1] => A
        )

    [9] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )

    [10] => Array
        (
            [0] => A
            [1] => C
            [2] => B
        )

    [11] => Array
        (
            [0] => B
            [1] => A
            [2] => C
        )

    [12] => Array
        (
            [0] => B
            [1] => C
            [2] => A
        )

    [13] => Array
        (
            [0] => C
            [1] => A
            [2] => B
        )

    [14] => Array
        (
            [0] => C
            [1] => B
            [2] => A
        )

)

Ora la mia domanda è, come posso pulire tale matrice in modo che:

array ( C, B )
is the same as
array ( B, C )

e rimuove il secondo array

Come dovrei farlo?

EDIT ... dopo alcune ricerche in base alle risposte, questo è ciò che mi si avvicinò con:

array_walk($array, 'sort');
$array = array_unique($array);

sort($array); // not necessary
È stato utile?

Soluzione

sorta le matrici costitutive:

foreach ($arrays AS &$arr)
{
   sort($arr);
}

{ "C", "B"} diventa => { "B", "C"}
e { "B", "C"} diventa => { "B", "C"}

che sono identici.

Altri suggerimenti

array_multisort($array);
array_unique($array);

È anche possibile utilizzare il pacchetto Math_Combinatorics .

require_once 'Combinatorics.php';
$combinatorics = new Math_Combinatorics;
$a = array('a', 'b', 'c');

// creating and storing the combinations
for($combinations = array(), $n=1; $n<=count($a); $n++) {
  $combinations = array_merge($combinations, $combinatorics->combinations($a, $n));
}

// test output
foreach($combinations as $c) {
  echo join(', ', $c), "\n";
}

stampe

a
b
c
a, b
a, c
b, c
a, b, c
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top