如何获取数据集的所有组合

时间:2018-03-27 17:58:23

标签: php

我有一组数据,我需要获得所有组合。

例如,我需要填写3个插槽,数字范围为1-5

我发现这个课程完全正常http://pyrus.sourceforge.net/Math_Combinatorics.html

$dataset = [
    1,2,3,4,5
];
$slots = 3;

$list = [];
require_once 'Combinatorics.php';
foreach((new Math_Combinatorics)->permutations($dataset, $slots) as $combination){
    $list[] = $combination;
}
print_r($list);

输出

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 2
        )

    [2] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 1
        )

    [3] => Array
        (
            [0] => 2
            [1] => 1
            [2] => 3
        )

    [4] => Array
        (
            [0] => 3
            [1] => 1
            [2] => 2
        )

    [5] => Array
        (
            [0] => 3
            [1] => 2
            [2] => 1
        )

    [6] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 4
        )

    [7] => Array
        (
            [0] => 1
            [1] => 4
            [2] => 2
        )

    [8] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 1
        )

    [9] => Array
        (
            [0] => 2
            [1] => 1
            [2] => 4
        )

    [10] => Array
        (
            [0] => 4
            [1] => 1
            [2] => 2
        )

    [11] => Array
        (
            [0] => 4
            [1] => 2
            [2] => 1
        )

    [12] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 5
        )

    [13] => Array
        (
            [0] => 1
            [1] => 5
            [2] => 2
        )

    [14] => Array
        (
            [0] => 2
            [1] => 5
            [2] => 1
        )

    [15] => Array
        (
            [0] => 2
            [1] => 1
            [2] => 5
        )

    [16] => Array
        (
            [0] => 5
            [1] => 1
            [2] => 2
        )

    [17] => Array
        (
            [0] => 5
            [1] => 2
            [2] => 1
        )

    [18] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 4
        )

    [19] => Array
        (
            [0] => 1
            [1] => 4
            [2] => 3
        )

    [20] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 1
        )

    [21] => Array
        (
            [0] => 3
            [1] => 1
            [2] => 4
        )

    [22] => Array
        (
            [0] => 4
            [1] => 1
            [2] => 3
        )

    [23] => Array
        (
            [0] => 4
            [1] => 3
            [2] => 1
        )

    [24] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 5
        )

    [25] => Array
        (
            [0] => 1
            [1] => 5
            [2] => 3
        )

    [26] => Array
        (
            [0] => 3
            [1] => 5
            [2] => 1
        )

    [27] => Array
        (
            [0] => 3
            [1] => 1
            [2] => 5
        )

    [28] => Array
        (
            [0] => 5
            [1] => 1
            [2] => 3
        )

    [29] => Array
        (
            [0] => 5
            [1] => 3
            [2] => 1
        )

    [30] => Array
        (
            [0] => 1
            [1] => 4
            [2] => 5
        )

    [31] => Array
        (
            [0] => 1
            [1] => 5
            [2] => 4
        )

    [32] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 1
        )

    [33] => Array
        (
            [0] => 4
            [1] => 1
            [2] => 5
        )

    [34] => Array
        (
            [0] => 5
            [1] => 1
            [2] => 4
        )

    [35] => Array
        (
            [0] => 5
            [1] => 4
            [2] => 1
        )

    [36] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
        )

    [37] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 3
        )

    [38] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 2
        )

    [39] => Array
        (
            [0] => 3
            [1] => 2
            [2] => 4
        )

    [40] => Array
        (
            [0] => 4
            [1] => 2
            [2] => 3
        )

    [41] => Array
        (
            [0] => 4
            [1] => 3
            [2] => 2
        )

    [42] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 5
        )

    [43] => Array
        (
            [0] => 2
            [1] => 5
            [2] => 3
        )

    [44] => Array
        (
            [0] => 3
            [1] => 5
            [2] => 2
        )

    [45] => Array
        (
            [0] => 3
            [1] => 2
            [2] => 5
        )

    [46] => Array
        (
            [0] => 5
            [1] => 2
            [2] => 3
        )

    [47] => Array
        (
            [0] => 5
            [1] => 3
            [2] => 2
        )

    [48] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 5
        )

    [49] => Array
        (
            [0] => 2
            [1] => 5
            [2] => 4
        )

    [50] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 2
        )

    [51] => Array
        (
            [0] => 4
            [1] => 2
            [2] => 5
        )

    [52] => Array
        (
            [0] => 5
            [1] => 2
            [2] => 4
        )

    [53] => Array
        (
            [0] => 5
            [1] => 4
            [2] => 2
        )

    [54] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
        )

    [55] => Array
        (
            [0] => 3
            [1] => 5
            [2] => 4
        )

    [56] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 3
        )

    [57] => Array
        (
            [0] => 4
            [1] => 3
            [2] => 5
        )

    [58] => Array
        (
            [0] => 5
            [1] => 3
            [2] => 4
        )

    [59] => Array
        (
            [0] => 5
            [1] => 4
            [2] => 3
        )

)

但是如果3个插槽中的每个插槽都有自己的数据集可供选择呢?

  • 插槽1:可以包含1-3
  • 插槽2:可能包含4-6
  • Slot 3:contian 7-9

如何从中获取所有组合?

重要

脚本必须是动态结构化的,因此您可以添加或删除一个或多个插槽

0 个答案:

没有答案