PHP中的组合,处置和排列

Ali*_*xel 6 php arrays algorithm math combinatorics

在PHP中生成数组的所有组合,配置和排列的最有效方法是什么?

Vic*_*Liu 7

以下是获取所有排列的代码:

http://php.net/manual/en/function.shuffle.php#90615

使用获得功率集的代码,排列是最大长度的排列,功率集应该是所有组合.我不知道什么是性格,所以如果你能解释它们,那会有所帮助.


let*_*cia 6

您可以使用此类:http://pear.php.net/package/Math_Combinatorics

并使用它像:

$combinatorics = new Math_Combinatorics;

$words_arr = array(
    'one'   => 'a',
    'two'   => 'b',
    'three' => 'c',
    'four'  => 'd',
    );

for ($i=count($words_arr)-1;$i>=1;$i--) {
    echo '<br><br>' . $i . ':<br>';
    $combinations_arr = $combinatorics->combinations($words_arr, $i);
    foreach ($combinations_arr as $combinations_arr_item) {
        echo implode(', ', $combinations_arr_item) . '<br>';
    }
}
Run Code Online (Sandbox Code Playgroud)