0

we have 3 school branches:

xschool atlanta 
xschool ortigas
xschool bagio

I will generate a simple combinations.
How can I generate the array which is below in php language

arr1[0]=> xschool atlanta
arr1[1]=>xschool ortigas
arr1[2]=>xschool bagio
arr1[3]=>xschool atlanta,xschool ortigas
arr1[4]=>xschool atlanta,xschool bagio
arr1[5]=>xschool ortigas,xschool bagio
arr1[6]=>xschool atlanta,xschool ortigas,xschool bagio

I wasn't able to set up algorithm in my mind...

Why I need this algorithm? Sometimes our parttime teachers can work in 2 branches different day. So when we add the teacher to our system, the combobox should show us the list(array) which is above.

Burhan
  • 13
  • 5
  • Why not simply use checkbox for school branches? – Maerlyn Oct 16 '14 at 08:02
  • Maerlyn, because school branches will be dynamic not static. In the future if we have 5 branches we will add the system 5 branches name so some of our teacher can work 3 branches as a part time teacher... so it should generate the list(array) which I explained already above. in combobox. if i choose "arr1[6]=>xschool atlanta,xschool ortigas,xschool bagio" it means this teacher work in these 3 branches. – Burhan Oct 16 '14 at 08:19

2 Answers2

2

I might provide you a direction (pseudocode), this is just one of many ways to solve this. You got to implement your own code ;).

Generate a list of binary number from 000 -> 111 Each number match to 1 permutation.

000 
001 => xschool atlanta
010 => xschool ortigas
100 => xschool bagio
110 => xschool atlanta,xschool ortigas
101 => xschool atlanta,xschool bagio
011 => xschool ortigas,xschool bagio
111 => xschool atlanta,xschool ortigas,xschool bagio

The rest you got to do by yourself. Have fun coding ;)

Toby D
  • 1,465
  • 1
  • 19
  • 31
0

Yes, I solved my problem. Here the code:

function combine_set_well($array) {
    // initialize by adding the empty set
    $results = array(array( ));

    foreach ($array as $element)
        foreach ($results as $combination)
            array_push($results, array_merge(array($element), $combination));

    return $results;
}

$set = array('xschool atlanta', 'xschool ortegas', 'xschool bagio');
$ayarla=combine_set_well($set);
asort($ayarla);
foreach ($ayarla as $combination) {
    print join(",", $combination) . "<br/>";
}
Burhan
  • 13
  • 5