如何使用PHP中每个组的两个元素从数组中获得所有可能的组合?

时间:2020-12-16 13:45:40

I'm stuck and putting my brain to work trying to solve this problem. I have this array:

我陷入了困境,绞尽脑汁试图解决这个问题。我有这个数组:

$options = array(
  'Group 1' => array( 'Track 1', 'Track 2', 'Track 3'),
  'Group 2' => array( 'Track 4', 'Track 5', 'Track 6'),
  'Gruop 3' => array( 'Track 7', 'Track 8', 'Track 9'),
  'Group 4' => array( 'Track 10', 'Track 11', 'Track 12'),
  'Group 5' => array( 'Track 13', 'Track 14', 'Track 15')
);

And I need to create a full set of 10 elements using two elements of each group following the ascendent order and avoiding a set with the same elements as another, like the example below:

我需要创建一个完整的10个元素集合,使用每个组的两个元素,遵循优势顺序,避免使用相同元素的集合,比如下面的例子:

I'm actually able to get only one item from each group:

我实际上只能从每组得到一项:

function create_set( $t, $terms, $i ) {
    $text = '';
    if ( $i >= count( $terms ) ) {
        $text .= trim( $t ) . "\n";
    }
    else {
        foreach ( $terms[$i] as $term ) {
            $text .= create_set( $t . $term . '##', $terms, $i + 1 );
        }
    }

    return $text;
}

$options = array(
 'Group 1' => array( 'I am track 1', 'I am track 2', 'I am track  3'),
 'Group 2' => array( 'I am track 4', 'I am track 5', 'I am track 6'),
 'Gruop 3' => array( 'I am track 7', 'I am track 8', 'I am track 9'),
 'Group 4' => array( 'I am track 10', 'I am track 11', 'I am track 12'),
 'Group 5' => array( 'I am track 13', 'I am track 14', 'I am track 15')
);

$combine = array();
foreach( $options as $k => $v ) {
    $combine[] = $v;
}

$text = create_set( '', $combine, 0 );
$text = preg_split( '/\n/', $text, -1, PREG_SPLIT_NO_EMPTY );
$combinations = array();
foreach( $text as $k => $v ) {
   $combinations[] = preg_split( '/##/', $v, -1, PREG_SPLIT_NO_EMPTY );
}
echo "<pre>";
print_r( $combinations );
echo "</pre>";
exit;   

Result:

结果:

 Array
(
[0] => Array
    (
        [0] => I am track 1
        [1] => I am track 4
        [2] => I am track 7
        [3] => I am track 10
        [4] => I am track 13
    )

Thank you very much for any help.

非常感谢你的帮助。

1 个解决方案

#1


0  

Since I do not know how the correct output should be I created this code:

由于我不知道正确的输出应该是什么,所以我创建了这个代码:

$result = [];
foreach ($options as $tracklist) {
    foreach (array_rand($tracklist, 2) as $key) {
        $result[] = 'I am '.$tracklist[$key];
    }
}

Output is

输出是

Array
(
    [0] => I am Track 1
    [1] => I am Track 3
    [2] => I am Track 4
    [3] => I am Track 5
    [4] => I am Track 7
    [5] => I am Track 9
    [6] => I am Track 11
    [7] => I am Track 12
    [8] => I am Track 13
    [9] => I am Track 14
)

#1


0  

Since I do not know how the correct output should be I created this code:

由于我不知道正确的输出应该是什么,所以我创建了这个代码:

$result = [];
foreach ($options as $tracklist) {
    foreach (array_rand($tracklist, 2) as $key) {
        $result[] = 'I am '.$tracklist[$key];
    }
}

Output is

输出是

Array
(
    [0] => I am Track 1
    [1] => I am Track 3
    [2] => I am Track 4
    [3] => I am Track 5
    [4] => I am Track 7
    [5] => I am Track 9
    [6] => I am Track 11
    [7] => I am Track 12
    [8] => I am Track 13
    [9] => I am Track 14
)