0

これは単純なことですが、私の脳は一日の終わりまでに揚げられます。助けてください:

<?php

    $arr = array(
        array(1, 2, 3),
        array(10, 20, 30),
        array(100, 200, 300),
    );

?>

すべての内部配列の各要素は、各順列ごとに1回しか使用できないという例外を除いて、内部配列のすべての可能な順列が必要です。例えば:

良い:

1-10-100
1-100-10
200-1-10

悪い:

1-2-3
30-20-200

元のレイアウトは変更できます。

4

3 に答える 3

2

これは、任意の数のアレイで機能します。

<?php

$arr = array(
    array(1, 2, 3),
    array(10, 20, 30),
    array(100, 200, 300),
    array(1000, 2000, 3000)
);

function permute($a, $b) {
    $perms = array();
    foreach($a as $va) {
        foreach($b as $vb) {
            $perms[] = sprintf('%s-%s', $va, $vb);
        }
    }
    return $perms;
}

$i=2;
$perms = permute($arr[0], $arr[1]);
while($i<count($arr)) {
    $perms = permute($perms, $arr[$i++]);
}

print_r($perms);
于 2013-03-05T22:47:07.830 に答える
1

これは、任意の数のアレイで機能するはずです。

<?php
$arr = array(
    array(1, 2, 3),
    array(10, 20, 30),
    array(100, 200, 300),
);
print_r(foo(array(), $arr));

function foo($arr1 = array(), $arr2 = array()) {
    $perm_arr = array();

    // case 1: empty array vs outer array
    // eg: array() vs array(array(1, 2, 3), array(10, 20, 30), array(100, 200, 300))
    // recursively reduce the size of outer array via case 2
    if(empty($arr1) && (isset($arr2[0]) && is_array($arr2[0]))) {
        $arr_top = array();
        $arr_bot = $arr2;

        // get possible arrangements for further reduction
        // eg:
        // array(1, 2, 3) vs array(array(10, 20, 30), array(100, 200, 300)),
        // array(10, 20, 30) vs array(array(1, 2, 3), array(100, 200, 300)),
        // array(100, 200, 300) vs array(array(1, 2, 3), array(10, 20, 30)),
        // ...
        while(0 < count($arr_bot)) {
            $arr = array_shift($arr_bot);
            $arr_edge = array_merge($arr_top, $arr_bot);
            $foo_arr_edge = foo($arr, $arr_edge);

            // append permutations of reduced array
            foreach($foo_arr_edge as $e_edge) {
                $perm_arr[] = $e_edge;
            }

            $arr_top[] = $arr;
        }
    }
    // case 2: inner array vs outer array
    // eg: array(1, 2, 3) vs array(array(10, 20, 30), array(100, 200, 300))
    // reduce the size of outer array until it reaches the base case
    else if(isset($arr1[0]) && (isset($arr2[0]) && is_array($arr2[0]))) {
        $foo_arr2 = array();

        $n_arr2 = count($arr2);
        // if the size of outer array is greater than 2 then reduce it until the size is 2
        if(2 < $n_arr2) {
            $arr_top = array();
            $arr_bot = $arr2;

            // get possible arrangements for further reduction
            // eg:
            // array(1, 2, 3) vs array(10, 20, 30),
            // array(1, 2, 3) vs array(100, 200, 300),
            // array(10, 20, 30) vs array(100, 200, 300),
            // ...
            while(0 < count($arr_bot)) {
                $arr = array_shift($arr_bot);
                $arr_edge = array_merge($arr_top, $arr_bot);
                $foo_arr_edge = foo($arr, $arr_edge);

                foreach($foo_arr_edge as $e_edge) {
                    $foo_arr2[] = $e_edge;
                }

                $arr_top[] = $arr;
            }
        }
        // if the size of outer array is 2 then get the permutations of its elements via the base case
        else if(2 == $n_arr2) {
            $foo_arr2 = foo($arr2[0], $arr2[1]);
        }

        // generate permutations from reduced array
        foreach($arr1 as $e1) {
            foreach($foo_arr2 as $e2) {
                $perm = $e1 .'-'. $e2;
                $perm_arr[] = $perm;
            }
        }
    }
    // base case: inner array vs inner array
    // eg: array(1, 2, 3) vs array(10, 20, 30)
    // generate permutations of two inner arrays
    else if(isset($arr1[0]) && (isset($arr2[0]) && !is_array($arr2[0]))) {
        foreach($arr1 as $e1) {
            foreach($arr2 as $e2) {
                $perm = $e1 .'-'. $e2;
                $perm_arr[] = $perm;
            }
        }
        foreach($arr2 as $e2) {
            foreach($arr1 as $e1) {
                $perm = $e2 .'-'. $e1;
                $perm_arr[] = $perm;
            }
        }
    }

    return $perm_arr;
}
?>
于 2013-03-06T01:11:42.610 に答える
0

私はこれがあなたが探している答えだと信じています:

<?php

$nL =   '
';
$arr = array(
        array(1, 2, 3),
        array(10, 20, 30),
        array(100, 200, 300),
    );

foreach ($arr[0] as $key => $val1) {
    foreach ($arr[1] as $val2) {
        foreach ($arr[2] as $val3) {
            echo $val1 . '-' . $val2 . '-' . $val3 . $nL;
        }
    }
}

foreach ($arr[0] as $key => $val1) {
    foreach ($arr[2] as $val2) {
        foreach ($arr[1] as $val3) {
            echo $val1 . '-' . $val2 . '-' . $val3 . $nL;
        }
    }
}

foreach ($arr[1] as $key => $val1) {
    foreach ($arr[0] as $val2) {
        foreach ($arr[2] as $val3) {
            echo $val1 . '-' . $val2 . '-' . $val3 . $nL;
        }
    }
}

foreach ($arr[1] as $key => $val1) {
    foreach ($arr[2] as $val2) {
        foreach ($arr[0] as $val3) {
            echo $val1 . '-' . $val2 . '-' . $val3 . $nL;
        }
    }
}

foreach ($arr[2] as $key => $val1) {
    foreach ($arr[0] as $val2) {
        foreach ($arr[1] as $val3) {
            echo $val1 . '-' . $val2 . '-' . $val3 . $nL;
        }
    }
}

foreach ($arr[2] as $key => $val1) {
    foreach ($arr[1] as $val2) {
        foreach ($arr[0] as $val3) {
            echo $val1 . '-' . $val2 . '-' . $val3 . $nL;
        }
    }
}

echo    'finished';
?>

楽しみ。=)

于 2013-03-05T22:18:33.050 に答える