0

I need to make a combination of all course input by the user such as the user add 5 courses it will be as this template: Course 1 Course 2 Course 3 Course 4 Course 5

After that, I need to create a combination for all course with array of grade:

array('A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F')

Actually, I found what I need in this topic: Create every possible combination in PHP but there's something I don't understand how I can do.

Until now this code after edit:

$dict = [
    '1' => ['A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F'],
    '2' => ['Course 1', 'Course 2', 'Course 3', 'Course 4', 'Course 5'],
];

$str = '2 : 1';

class SentenceTemplate implements IteratorAggregate
{
    private $template;
    private $thesaurus;

    public function __construct($str, $dict)
    {
        $this->thesaurus = [];

        $this->template = preg_replace_callback('/\w+/', function($matches) use ($dict) {
            $word = $matches[0];
            if (isset($dict[$word])) {
                $this->thesaurus[] = $dict[$word];
                return '%s';
            } else {
                return $word;
            }
        }, $str);
    }

    public function getIterator()
    {
        return new ArrayIterator(array_map(function($args) {
            return vsprintf($this->template, $args);
        }, $this->combinations($this->thesaurus)));
    }

    private function combinations($arrays, $i = 0) {
        if (!isset($arrays[$i])) {
            return array();
        }
        if ($i == count($arrays) - 1) {
            return $arrays[$i];
        }

        // get combinations from subsequent arrays
        $tmp = $this->combinations($arrays, $i + 1);

        $result = array();

        // concat each array from tmp with each element from $arrays[$i]
        foreach ($arrays[$i] as $v) {
            foreach ($tmp as $t) {
                $result[] = is_array($t) ? array_merge(array($v), $t) : array($v, $t);

            }
        }

        return $result;
    }
}

$sentences = new SentenceTemplate(1, $dict);
$i = 1;
foreach ($sentences as $sentence) {
    echo "COMBINATION $i : \n";
    $sentences2 = new SentenceTemplate(2, $dict);
    foreach ($sentences2 as $sentence2) {
            echo "$sentence2 : "."$sentence\n";
    }
    echo "----------\n";
    $i++;
}

Output

COMBINATION 1 : 
Course 1 : A+
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
----------
COMBINATION 2 : 
Course 1 : A
Course 2 : A
Course 3 : A
Course 4 : A
Course 5 : A
----------
COMBINATION 3 : 
Course 1 : B+
Course 2 : B+
Course 3 : B+
Course 4 : B+
Course 5 : B+
----------
COMBINATION 4 : 
Course 1 : B
Course 2 : B
Course 3 : B
Course 4 : B
Course 5 : B
----------
COMBINATION 5 : 
Course 1 : C+
Course 2 : C+
Course 3 : C+
Course 4 : C+
Course 5 : C+
----------
COMBINATION 6 : 
Course 1 : C
Course 2 : C
Course 3 : C
Course 4 : C
Course 5 : C
----------
COMBINATION 7 : 
Course 1 : D+
Course 2 : D+
Course 3 : D+
Course 4 : D+
Course 5 : D+
----------
COMBINATION 8 : 
Course 1 : D
Course 2 : D
Course 3 : D
Course 4 : D
Course 5 : D
----------
COMBINATION 9 : 
Course 1 : F
Course 2 : F
Course 3 : F
Course 4 : F
Course 5 : F
----------

And now how I can build more combinations such as:

COMBINATION 10 : 
Course 1 : B+
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 11 : 
Course 1 : B
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 12 : 
Course 1 : C+
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 13 : 
Course 1 : C
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 14 : 
Course 1 : D+
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 15 : 
Course 1 : D
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 16 : 
Course 1 : F
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
.......

The procedure is for every course has a different grade with other courses and other courses has the same procedure, anyone has an idea to build this?

  • does the number of the `COMBINATION` have a special meaning or pattern, or is just a formatting thing? – William Perron Aug 26 '17 at 15:03
  • 1
    Possible duplicate of [How to generate in PHP all combinations of items in multiple arrays](https://stackoverflow.com/questions/8567082/how-to-generate-in-php-all-combinations-of-items-in-multiple-arrays) – k0pernikus Aug 26 '17 at 21:02

1 Answers1

0

This code may help. It is a PHP program to print all possible strings of length k.

<?php
    // PHP program to print all possible strings of length k

    // Driver method to test below methods
    function main() {            
        $set = ['A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F'];
        $k = 5;
        $n = sizeof($set);
        printAllKLengthRec($set, "", $n, $k);
    }

    // The main recursive method to print all possible strings of length k
    function printAllKLengthRec($set, $prefix, $n, $k) {

        // Base case: k is 0, print prefix
        if ($k == 0) {
            echo $prefix."\n";
            return;
        }

        // One by one add all characters from set and recursively 
        // call for k equals to k-1
        for ($i = 0; $i < $n; ++$i) {

            // Next character of input added
            $newPrefix = $prefix . $set[$i]; 

            // k is decreased, because we have added a new character
            printAllKLengthRec($set, $newPrefix, $n, $k - 1); 
        }
    }

    main();
?>

You just need to modify the printing part. The code bellow may fulfill your purpose.

<?php
    // PHP program to print all possible strings of length k

    // Driver method to test below methods
    function main() {            
        $set = ['A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F'];
        $k = 5;
        $n = sizeof($set);
        printAllKLengthRec($set, "", $n, $k);
    }

    // The main recursive method to print all possible strings of length k
    function printAllKLengthRec($set, $prefix, $n, $k) {

        // Base case: k is 0, print prefix
        if ($k == 0) {
            $c = 1;
            for($p=0; $p<strlen($prefix); $p++) {
                echo "Course $c: ". $prefix[$p];
                if(($p+1) < strlen($prefix) && ($prefix[$p+1] == '+' || $prefix[$p+1] == '-')) {
                    echo $prefix[$p+1];
                    $p++;
                }
                echo "\n";
                $c++;
            }
            return;
        }

        // One by one add all characters from set and recursively 
        // call for k equals to k-1
        for ($i = 0; $i < $n; ++$i) {

            // Next character of input added
            $newPrefix = $prefix . $set[$i]; 

            // k is decreased, because we have added a new character
            printAllKLengthRec($set, $newPrefix, $n, $k - 1); 
        }
    }

    main();
?>
Mahbubul Islam
  • 998
  • 1
  • 10
  • 24
  • Thanks!, when I edit the output will be as this way : `Course 1 : A+ Course 1 : A+ Course 1 : A+ Course 1 : A+ Course 1 : A+` and I need the output like this: `Course 1 : A+ Course 2 : A+ Course 3 : A+ Course 4 : A+ Course 5 : A+` – Mohammad Okfie Aug 26 '17 at 21:33
  • Thank , Nice ans ... :) – Mahidul Islam Sep 26 '17 at 11:05