-1

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:

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.

Alrogatto
  • 33
  • 7

1 Answers1

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
)
Philipp Palmtag
  • 1,310
  • 2
  • 16
  • 18
  • Thank you, Philipp. I inserted more information on my first post. – Alrogatto Aug 17 '16 at 14:47
  • This solution is not correct? Do you want the first two elements in the group array or has `Track` to be lowercase? – Philipp Palmtag Aug 17 '16 at 15:00
  • I'll try to explain, maybe i'm not knowing how to communicate. I have five groups with 3 tracks each one. I need to form sets with 10 tracks each, using two tracks from each group, and these sets can't have the same 10 tracks as another. I hope I had communicated it well. :) Thank you!! – Alrogatto Aug 17 '16 at 15:12
  • I dont understand how you want to form multiple sets with 10 unique elements if you only have 15 elements. If I understand you want this. Example only for Group one: Set 1: (1,2), Set 2: (1,3), Set 3:(2,1), Set 4: (2,3), Set 5: (3,1), Set 6 (3,2). Is that correct? – Philipp Palmtag Aug 18 '16 at 05:53
  • Hello. Each set with 10 elements cannot contain 10 identical elements from other set. And to form these sets, we can use only two elements from each group. Each group have three options. We have to choose two. So, each group will have the total of 3 formations. Ie: Group 1 formations: [Track 1, Track 2] ; [Track 1, Track 3]; [Track 2, Track 3]. Now we need to mix with the other groups and create sets with 10 elements. 5 groups, 3 formations each group. 3 * 3 * 3 * 3 * 3 = 243. We will have exactly 243 sets. Thank you for your patience! – Alrogatto Aug 18 '16 at 11:33
  • You might want so search for "array combination" or "array permutation" on StackOverflow. I found this solution helpful, but it does not work on your specific case, you have to change it to get two tracks instead of one: http://stackoverflow.com/a/8567199/5503220 – Philipp Palmtag Aug 19 '16 at 08:19